【问题标题】:Axios Post Works In Debug But Gives "Network Error" In Android Release BuildAxios Post 在调试中工作,但在 Android 发布版本中出现“网络错误”
【发布时间】:2019-10-19 01:30:56
【问题描述】:

我正在开发一个 React Native 应用程序。我正在使用 Axios 对 PHP Laravel 中的服务器进行 api 调用。如果我使用调试版本运行应用程序,一切都会按预期工作。我可以毫无问题地调用服务器。当我尝试发布 apk 时,Axios 总是给出状态为 0 的“网络错误”。我已经确认我在服务器上也没有收到任何请求。但这只发生在 Release apk 中。

我使用本地 ip 192.168.0.112,服务器在端口 80 上运行。我也尝试了不同的端口,但无济于事。问题是,如果我可以在调试 apk 中调用服务器,那么发布 apk 立即给出“网络错误”的问题是什么?

//这是当用户//按下登录按钮时我在第一次启动应用程序时进行的第一次调用,这是失败的地方并且catch中的代码运行。

Axios.post(server + "app/login", {
    email: user.userEmail.toLowerCase(),
    password: user.userPassword
    })
      .then(function(response) {
        //This part doesn't run in release but works fine in debug
        if (response.data.access_token.length) {
        //Do Stuff If Logged In
        }
    })
      .catch(function(error) {
      //This is always executed in release as soon as the request is made
      //i.e as soon as I press login button to login
      console.log(error.request.status, error.config.url); 
      // Gives status = 0 and url "http://192.168.0.112/app/login"
    });

预期的输出应该是应用程序允许我登录以获得正确的凭据,并在我输入错误凭据时给我“错误的电子邮件或密码”消息。而且我确实在调试中完美地得到了这个输出。该错误仅发生在 Release Apk 中。

【问题讨论】:

    标签: javascript android react-native axios


    【解决方案1】:

    在清单文件中进行以下更改

       <manifest xmlns:tools="http://schemas.android.com/tools"> 
            <application android:usesCleartextTraffic="true" tools:targetApi="28"> 
            </application>
         </manifest>
    

    【讨论】:

      【解决方案2】:

      我找到了这个问题的答案,如下所示: https://github.com/facebook/react-native/issues/23986#issuecomment-475783025

      【讨论】:

        猜你喜欢
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 2020-10-26
        • 1970-01-01
        • 2022-09-30
        • 1970-01-01
        • 1970-01-01
        • 2017-11-26
        相关资源
        最近更新 更多