首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >反应原生Axios在iOS中工作,但不能在安卓中调用

反应原生Axios在iOS中工作,但不能在安卓中调用
EN

Stack Overflow用户
提问于 2020-01-22 03:15:46
回答 3查看 2.7K关注 0票数 0

我似乎不能让下面的Axios调用在Android上工作。它没有命中服务器并显示错误: Error: Network Error。同样的代码也适用于iOS。我需要做一些特殊的事情才能在React Native上启用Android的网络请求吗?

代码语言:javascript
运行
复制
 var url = 'https://***.****.com/api/list';
 axios.get(url, 
           { headers: {'Content-Type': 'application/json'}}
          ).then((response) => {
               console.log("response = ",response.data)
          })
          .catch((error) => {
              console.log("error = ", error)
          });

我在控制台中得到了这个错误:

代码语言:javascript
运行
复制
error = >  Error: Network Error
    at createError (index.bundle?platfor…minify=false:117949)
    at XMLHttpRequest.handleError (index.bundle?platfor…minify=false:117857)
    at XMLHttpRequest.dispatchEvent (index.bundle?platfor…&minify=false:32349)
    at XMLHttpRequest.setReadyState (index.bundle?platfor…&minify=false:31433)
    at XMLHttpRequest.__didCompleteResponse (index.bundle?platfor…&minify=false:31260)
    at index.bundle?platfor…&minify=false:31370
    at RCTDeviceEventEmitter.emit (index.bundle?platfor…e&minify=false:5652)
    at MessageQueue.__callFunction (index.bundle?platfor…e&minify=false:5080)
    at index.bundle?platfor…e&minify=false:4793
    at MessageQueue.__guard (index.bundle?platfor…e&minify=false:5034)
EN

回答 3

Stack Overflow用户

发布于 2020-01-22 03:36:00

iOS和Android在过去都为我工作过。我使用的一个基本框架是首先用一个helper文件初始化一个axios对象:

代码语言:javascript
运行
复制
// test_api.js
import axios from 'axios';

export default axios.create({
  baseURL: 'https://***.****.com/api/list',
  headers: {'Content-Type': 'application/json'}
});

拉取数据文件中的...then:

代码语言:javascript
运行
复制
// app.js
import test_api from './test_api';

const request = async () => {
  try {
    const response = await test_api.get('/');
    console.log("response = ", response.data)
  } catch (error) {
    console.log("error = ", error)
  };

希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2020-02-20 01:48:30

可能是您的错误类似于此问题:https://stackoverflow.com/a/16302527/7714108

你需要更深入的了解你的错误对象,如果你

javax.net.ssl.SSLHandshakeException:

找不到证书路径的java.security.cert.CertPathValidatorException:信任密钥。

您的错误是关于错误配置的服务器。

我在Android 6.0 - API 23 (Marsmallow)和更早的版本上遇到了这个问题。我不知道为什么这些api和更早的版本只发生在我身上。我正在使用Expo SDK35,我尝试了axios,fetch和XMLHttpRequest,错误总是非常类似的“网络错误”。但除此之外,我正在使用React Native Debugger进行调试,然后我的好奇心是,为什么当我启用Network Inspect时,这个错误就消失了。

票数 0
EN

Stack Overflow用户

发布于 2020-02-21 14:16:46

请不要使用"axios“而不是"fetch”。它适用于iOS和安卓系统,下面是获取数据的示例代码。

代码语言:javascript
运行
复制
  Test = async ()=>
    {
      try
      {
     fetch(
          'your url', 
          {
          method: 'POST',
           headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
          body: JSON.stringify({
             firstParam: 'yourValue',
        secondParam: 'yourOtherValue',

          })
        }).then(response => {
          const statusCode = response.status;
          const data = response.json();
          return Promise.all([statusCode, data]);
        })
        .then((res) => {
          if (res[0] == 200 && res[2].request_results.result_header.code == 100)
          {

          }
    }
        }).catch((error) =>
          {
            console.error(error);
          }
        );
        }
      }
      catch (e)
      {

      }

      };

您将在"res“中获取数据,并可以使用for-loop或其他您想要应用的逻辑来提取数据。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59847802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档