React-native POST request in android over https return network error

假如想象 提交于 2019-12-10 11:32:48

问题


Tried to create user account through https POST request under react-native with axios, while it always failed on android with a 'network error'.

axios('https://'+DEST_URI, {
 method: 'post',
 data: account,
 headers: {
   'Accept': 'application/json',
 }
 then(...)

The same https POST request works fine on iOS.

Changed to http, the same POST request works on android as well.

Also tried GET request through https on android, it could retrieve data from the backend server as expected.

Any idea about it?

Linked image is the output from console log, output from console log


回答1:


Related to the issue Upgrade to OkHttp3 on the react-native repository.

Deactivate http2 from your proxy that is what causing the problem. You can try to upgrade to react-native v0.27 they mention in the change log that they fixed this problem but for me didn't work.

I had to disable the http2 feature in ngnix to make it work.

disabling http2 really is this simple, just change this line in your ngnix config file from:

listen 443 ssl http2;

to

listen 443 ssl;

then reload your config:

service nginx reload

More info about http2 and ngnix can be found here




回答2:


I was facing the same issue with android 'POST' requests. Turned out to be headers issue as mentioned in the link: https://github.com/facebook/react-native/issues/5222#issuecomment-170239302

Adding the following headers fixed the issue for me

headers = {
'Accept': 'application/json',
'Authorization': 'Bearer ' + this.authToken,
'Content-Type': 'application/x-www-form-urlencoded',
}



回答3:


I guess you have not a internet permission in your AndroidManifest.xml file.

Please add this line into you AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />



来源:https://stackoverflow.com/questions/38171170/react-native-post-request-in-android-over-https-return-network-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!