Axios Post 可在调试中运行,但出现“网络错误"在 Android 发布版本中 [英] Axios Post Works In Debug But Gives "Network Error" In Android Release Build

查看:57
本文介绍了Axios Post 可在调试中运行,但出现“网络错误"在 Android 发布版本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a React Native App. I'm using Axios to make api calls to server which is in PHP Laravel. If I run the app with the debug build everything works as expected. I can make calls to server with no problem. When I try the release apk, Axios always gives "Network Error" the status for which is 0. I've confirmed that I don't receive any request on the server aswell. But this only happens in Release apk.

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

Im using local ip 192.168.0.112 on which the server is running on port 80. I've tried different ports aswell to no avail. The thing is that if I can make calls to server in debug apk then what's the issue with the release apk for instantly giving "Network Error"?

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

//This is the first call that I make in the first launch of app when user //presses Login button, this is where it fails and the code in catch runs.

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"
    });

预期的输出应该是该应用程序让我登录以获取正确的凭据,并在我输入错误凭据时给我消息错误的电子邮件或密码".我确实在 Debug 中完美地得到了这个输出.该错误仅发生在 Release Apk 中.

Expected output should be that the app lets me login for correct credentials and give me the message "Wrong Email or Password" as I've put in for wrong credentials. And I do get this output in Debug perfectly. The error occurs only in Release Apk.

推荐答案

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

Do below change in your manifest file

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

这篇关于Axios Post 可在调试中运行,但出现“网络错误"在 Android 发布版本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆