5

Fetching non-https url are failing after updating sdk version to 26. This happens only in production build. In development mode everything works fine. Here is the piece of code:

fetch('http://something.com/').then(
  r => {},
  e => console.error(e));

This produces the following in logcat:

08-12 19:08:47.555 14586 14623 E ReactNativeJS: [TypeError: Network request failed]

Here is my android/app/build.gradle file:

android {
    compileSdkVersion 23
    buildToolsVersion "27.0.3"

    defaultConfig {
        ...

        minSdkVersion 16
        targetSdkVersion 26
        versionCode 28
        versionName "0.1.0"
    }

...
}
Olim Saidov
  • 2,796
  • 1
  • 25
  • 32
  • 1
    Android 9.0+ [bans cleartext traffic by default](https://developer.android.com/about/versions/pie/android-9.0-changes-28#framework-security-changes) (see the "Network TLS enabled by default") for apps with a `targetSdkVersion` of 28 or higher. Ideally, you switch to accessing sites using the `https` scheme. – CommonsWare Aug 12 '18 at 14:24
  • @CommonsWare I am sorry. Sdk version I have currently is 26. I'm testing on an android 7.0 – Olim Saidov Aug 12 '18 at 14:49
  • I've included contents of `build.gradle` file. – Olim Saidov Aug 15 '18 at 14:51
  • 1
    I believe device blocks non secure urls by default. you should find for a way to access non secure web-content within native configs. – Jimi Pajala Aug 20 '18 at 13:07

3 Answers3

1

Try with some other http client, for example axios

pavle
  • 909
  • 14
  • 38
0

You might need to add INTERNET permissions to your AndroidManifest.xml.

Like this:

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

Another reason might be that your request contains charset: utf-8 in the header. This can be a problem if you ping non-latin servers. In that case your server admin needs to update his nginx (or whatever he uses for web-serving) settings to allow that header.

Last but not least the problem might be a virtual router.

J. Hesters
  • 13,117
  • 31
  • 133
  • 249
-2

The thing is, that iOS is running in a simulator and Android is running in an emulator. The localhost is pointing to the environment in which the code is running. The emulator emulates a real device while the simulator is only imitating the device. Therefore the localhost on Android is pointing to the emulated Android device. And not to the machine on which your server is running. The solution is to replace localhost with the IP address of your machine.

chel long
  • 1
  • 1