facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.28k stars 24.35k forks source link

"NETWORK ERROR" when making API call in react-native Android, while works in iOS #31045

Open prayaslashkari opened 3 years ago

prayaslashkari commented 3 years ago

Please provide all the information requested. Issues that do not follow this format are likely to stall.

Description

API calls not working in Android devices, while its working in iOS simulator and device

My BaseUrl -> 'http://167.XXX.XX.89'

My network_security_config.xml image

I've included this file (network_security_config.xml) in AndroidManifest.xml (android/app/src/main) too.

My FLIPPER_VERSION image

Also I've commented out image

API calls are successfully working in iOS devices but not working in android.

ERROR image

React Native version:

image

System: OS: macOS 11.2.1 CPU: (8) x64 Apple M1 Memory: 33.83 MB / 8.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.15.3 - /usr/local/bin/node Yarn: Not Found npm: 6.14.9 - /usr/local/bin/npm Watchman: Not Found Managers: CocoaPods: 1.10.1 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: iOS 14.4, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2 Android SDK: API Levels: 29 Build Tools: 28.0.3, 29.0.2 System Images: android-29 | Intel x86 Atom_64 Android NDK: Not Found IDEs: Android Studio: 4.1 AI-201.8743.12.41.7042882 Xcode: 12.4/12D4e - /usr/bin/xcodebuild Languages: Java: 1.8.0_282 - /usr/bin/javac Python: 2.7.16 - /usr/bin/python npmPackages: @react-native-community/cli: Not Found react: ^16.13.1 => 16.14.0 react-native: ^0.63.2 => 0.63.4 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. Tried making API calls in Android.
  2. Its not working, its working in iOS devices.

Expected Results

Successful API Calls.

Snack, code example, screenshot, or link to a repository:

Please provide a Snack (https://snack.expo.io/), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem. You may provide a screenshot of the application if you think it is relevant to your bug report. Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

roycechua commented 3 years ago

Did you try adding android:usesCleartextTraffic="true" to your AndroidManifest.xml yet?

Screen Shot 2021-03-03 at 10 28 33 AM

By default Android does not permit HTTP request on its latest OS versions. I think network security config is different from actually setting it in the application manifest. I ran into this problem a couple of months ago and this is what worked for me.

You can refer here for more details:

yusufinthehouse commented 3 years ago
  • Axios doesn't work with Android (emulator) raising a Network Error axios/axios#973 (comment)

It doesn't work with 0.64.2. I always got Network Error response for each call.

Lloyd1229 commented 3 years ago

Did you solve it? How to do it

It doesn't work with 0.64.2. I always got Network Error response for each call.

anastely commented 3 years ago

Did you solve it? @Lloyd1229 @yusufinthehouse

yusufinthehouse commented 3 years ago

Did you solve it?

@Lloyd1229 @yusufinthehouse

I reverted my code using 0.61.1 last time and it can work properly again.

I haven't upgraded the code again after that time.

But if I worked with fresh RN project. I cannot find this issue anymore.

mlk9 commented 3 years ago

I tried without SSL (http) worked. so I think android doesn't support this.

eemkays commented 3 years ago

We are facing same issue with Android and AVD Emulator. All API calls are giving Network Error. We are using our Dev Server for connectivity for APIs. The Dev Server URL looks like: https://dev.domain.com React: 0.64.0 React Native: 17.0.1 NPM/NPX: 6.14.15 Node: 12.22.7 Android 11 (SDK 30) Device: Pixel 5

For iOS all API calls are working fine.

RupamShaw commented 2 years ago

resolved it by app/build.gradle

    implementation 'com.squareup.okhttp3:okhttp:4.9.1'
    implementation("com.squareup.okhttp3:okhttp-urlconnection:4.9.1")
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'

have build.gradle

    buildToolsVersion = "29.0.3"
        minSdkVersion = 21
        compileSdkVersion = 30
        targetSdkVersion = 30

having in package.json

 "axios": "^0.21.1",
 "expo": "^43.0.0",
 "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
norflin321 commented 2 years ago

upgrade from "react-native": "0.66.4" to "react-native": "0.67.2" solved it for me. Used "npx react-native upgrade".

BenjaminGolba commented 2 years ago

Have you guys tried using Flipper and the devices logs (Sidebar Device > Log). You should be getting the native log entry for that request which has a lot more information than just "Network Error".

emuter commented 2 years ago

i figured still some people are struggling with this issue. here's what you could do in windows environment.

  1. create a local domain.
  2. put the created domain name in etc host file.
  3. point the created domain to your pc in your network router ( better with internet connection )
  4. point the domain in your local dev server vhost file.

and... voila you got it running even without ssl

Ichigo1033 commented 1 year ago

yo cuento con el mismo error cuando intento consumir la api con axios y fetch el problema va de que intento subir una imagen a uns servidor, en el servidor ya voy consumiendo distintas API y funciona todo normal el problema que va es al subir la imagen al servidor con form data, intente varias soluciones en cuanto al codigo y nada el problema solo me da en android en la app de expo goo y tambien en el build de android si ejecuto npx expo start --web y subo la imagen al servidor funciona pero en la app de expo go en android. nota utilizo expo 47 alguna solucion este es mi formato de form data : const formData = new FormData()

formData.append('files', sendFile)
formData.append('FileName', nameImage)
formData.append('FolderName', 'Comprobantes')

await postUploadImage(formData);
espero que peudan ayudarme 
vikassinghss commented 1 year ago

how to fix in ios

github-actions[bot] commented 8 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

ArunKumar-webdev commented 8 months ago

any solution ?

meetfaisalmirza commented 2 months ago

any solution ?

jocoders commented 1 month ago

Guys any solitions? I have the same problem on "react-native": "0.72.17"

When I try to run adb shell ping -c 4 google.com, I get the following error:

ping: unknown host google.com

However, when I ping google.com directly from my system (macOS) terminal, everything works fine. Here's what I've tried so far:

Added various DNS addresses in the emulator settings. Restarted adb and the emulator multiple times. Reinstalled adb and the Android Emulator, but nothing has resolved the issue. Here are some logs related to network activity from adb logcat:

09-30 16:05:09.461  3544  4555 I ReactNativeJS: Network: Request: ID:5f3d098f-d095-449f-b73e-51e1f84b86a8 METHOD:POST URL: /mySite
09-30 16:05:09.875  3544  6285 D TrafficStats: tagSocket(151) with statsTag=0xffffffff, statsUid=-1
09-30 16:05:11.881  3544  6287 D TrafficStats: tagSocket(163) with statsTag=0xffffffff, statsUid=-1
09-30 16:05:13.886  3544  6289 D TrafficStats: tagSocket(118) with statsTag=0xffffffff, statsUid=-1
09-30 16:05:17.894  3544  6296 D TrafficStats: tagSocket(141) with statsTag=0xffffffff, statsUid=-1

Network Settings Analysis:

IP Addresses: Emulator has IP addresses 10.0.2.15 for eth0 and 10.0.2.16 for wlan0.
Routing: Routes for eth0 and wlan0 are correctly configured for the local network.
DNS: Executing ping on 8.8.8.8 (Google DNS) is successful, indicating that the network connection is functional up to the IP level, but DNS queries are not being executed.

Additionally, it is important to note that accessing and modifying network settings directly within the emulator is not possible. This limitation prevents any direct changes to DNS or other network configurations from within the emulator's system settings.

Possible Causes:

DNS servers are not configured or are incorrectly set in the emulator's network settings. Issues with the network stack configuration within the emulator itself. How to fix it?