Android encounters Network Error when uploading images using Axios in React Native

To make changes to your project, go to MainApplication.java in android/app/src/main/java/com/{yourProject}/ and comment out a specific line. In ReactNativeFlipper.java in android/app/src/debug/java/com/{yourProject}/, on line 43, comment in code for image upload.
There are three possible solutions to the problem. The first solution involves changing a certain attribute. The second solution can be found by following a link to a GitHub issue. The third solution involves updating your Flipper_Version to 0.41.0 or higher.
To enable https request, you can use an attribute to allow all requests in your AndroidManifest.xml. Another solution is to add a specific code to AndroidManifest.xml. Lastly, instead of using a certain command, use an alternative one for Android.


Solution 1:

  1. Open this dir ‘android/app/src/debug/java/com/flatApp/ReactNativeFlipper.java’

  2. Comment the line as per below code:

      NetworkingModule.setCustomClientBuilder(
       new NetworkingModule.CustomClientBuilder() {
         @Override
         public void apply(OkHttpClient.Builder builder) {
           // builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
         }
       });
     


Solution 2:


Experiencing the identical problem, I found that the accepted solution was ineffective. However, I discovered a method that assisted me.

  1. In android/app/src/main/java/com/{yourProject}/MainApplication.java
    comment the below line :

    initializeFlipper(this, getReactNativeHost().getReactInstanceManager())
     
  2. In android/app/src/debug/java/com/{yourProject}/ReactNativeFlipper.java
    comment in line 43 :

    builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
     
  3. Code for image upload :

    var formData = new FormData();
       formData.append('UserId', '[email protected]');
       formData.append('VisitId', '28596');
       formData.append('EvidenceCapturedDate', '09/10/2019 13:28:20');
       formData.append('EvidenceCategory', 'Before');
       formData.append('EvidenceImage', {
         uri: Platform.OS === 'android' ? `file:///${path}` : `/private${path}`,
         type: 'image/jpeg',
         name: 'image.jpg',
       });
       axios({
         url: UrlString.BaseUrl + UrlString.imageUpload,
         method: 'POST',
         data: formData,
         headers: {
           Accept: 'application/json',
           'Content-Type': 'multipart/form-data'
         },
       })
         .then(function (response) {
           console.log('*****handle success******');
           console.log(response.data);
         })
         .catch(function (response) {
           console.log('*****handle failure******');
           console.log(response);
         });
     


Solution 3:

FLIPPER_VERSION=0.41.0

Please ensure that your Flipper_Version is at or above 0.41.0.

Frequently Asked Questions