【问题标题】:https connection in Android react-native not working on physical device after buildAndroid react-native中的https连接在构建后无法在物理设备上运行
【发布时间】:2021-04-16 08:26:34
【问题描述】:

我使用 expo 和 Axios(用于服务器请求)创建了一个带有 react-native 的简单应用程序

在构建之前,在开发过程中,所有 https 请求都运行良好。

构建后,在物理设备上运行 apk 时,https 根本不起作用。

我在使用 Logcat 时遇到的错误是“网络错误”。 应用程序中的其他 Internet 连接(构建后)确实有效,例如打开网页的 webview 或 Firebase 连接。

    analyzerApi.post('/analyze', urls) .then((res) => { 
dispatch({type: 'get_result', payload: res.data.analysis})}).catch(err => console.log("Error in getting analyze.. " ,err.name, err.message))

(analyzerApi 是一个 axios 实例,baseUrl 指向我的服务器)

【问题讨论】:

    标签: android react-native mobile https expo


    【解决方案1】:

    此调用适用于 HTTP 和 HTTPS 尝试此示例用于 POST CALL

    fetch('http://mywebsite.com/endpoint/', {
          method: "POST",
          headers: {
            // Authorization: "Bearer " + Token,
            Accept: 'application/json',
          'Content-Type': 'application/json'
          },
          body: JSON.stringify({
           // sending data userID username, password, etc
          }),
        })
          .then((response) => response.json())
          .then((responseJson) => {
           // Response will here
          })
          .catch((error) => {
            alert(error);
          });
    

    试试这个 GET CALL 的例子

    fetch('http://mywebsite.com/endpoint/', {
          method: "GET",
          headers: {
           // Authorization: "Bearer " + Token,
           // OR USER name PASSworrd
           Accept: 'application/json',
          'Content-Type': 'application/json'
          },
        })
          .then((response) => response.json())
          .then((responseJson) => {
    
            // Response will here
          })
          .catch((error) => {
            alert(error);
          });
    

    【讨论】:

    • 现在用 fetch 代替 Axios 效果很好,你知道 Axios 有什么问题吗?
    • 我遇到了同样的问题,我通过研究找到了这个解决方案,所以我建议你
    猜你喜欢
    • 2020-11-16
    • 1970-01-01
    • 2017-01-14
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多