4

So basically what I'm doing is simply making simple Axios call to our UAT server which has HTTPS enabled. I've tested the entire application in IOS API calls are working fine there but as soon as I've tested the same application in Android (On a real Android device) Axios calls are failing with Network Error. I have tested the same thing on Different Android phones with different wifi/network connect but still the same issue.

Upon researching, I've found that it's happening due to an SSL certificate issue. So I'm using the react-native-ssl-pinning library. But in that also I`m facing this issue.

Anyone faced this before. Am I missing something here?

1 Answer 1

5

Step1 : create a java file ex: 'CustomClientFactory' inside directory app > Java > com > (your_app_name) > CustomClientFactory.java

Inside that java file add the following code:

import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.ReactCookieJarContainer;

import java.security.cert.CertificateException;
import java.util.ArrayList;

import java.util.List;

import java.util.concurrent.TimeUnit;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;

import static android.content.ContentValues.TAG;

public class CustomClientFactory implements OkHttpClientFactory {
private static final String TAG = "OkHttpClientFactory";
@Override
public OkHttpClient createNewNetworkModuleClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void 
checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws 
CertificateException {
                    }

                    @Override
                    public void 
checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws 
CertificateException {
                    }

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[]{};
                    }
                }
        };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();



        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(0, TimeUnit.MILLISECONDS).readTimeout(0, TimeUnit.MILLISECONDS)
                .writeTimeout(0, TimeUnit.MILLISECONDS).cookieJar(new ReactCookieJarContainer());
        builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        throw new RuntimeException(e);
    }
}

}

and inside your MainApplication.java add the following:

 @Override
 public void onCreate() {
 super.onCreate();
 SoLoader.init(this, /* native exopackage */ false);  
 OkHttpClientProvider.setOkHttpClientFactory(new CustomClientFactory()); //add this 
 line.
 } 

Found the above solution from : Similar issue

And it worked fine for me. As per I understand, we write this file to Install the all-trusting trust managers and asking the app to allow 'https' api

Sign up to request clarification or add additional context in comments.

3 Comments

Hi what package should i import to MainApplication.java?
@QrQr In MainApplication.java import import com.facebook.react.modules.network.OkHttpClientProvider; and inside void onCreate() add this line - OkHttpClientProvider.setOkHttpClientFactory(new CustomClientFactory());
Thank you man, I was dealing with this problem for Hours! My backend was working fine in IOS, but on Android I was facing network errors. I saw that was something to do with ssl, but couldnt make it work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.