Connection Timeout exception in com.loopj.android:android-async-http continuously

155 Views Asked by At

I'm using com.loopj.android:android-async-HTTP library for HTTP request. but sometimes response is coming but sometime not. when i switching internet to WiFi(jio hotspot) response is not coming and showing connect timeout exception. if i restart the internet response is coming. app is already in play store. so i can't change this library.please help me out.

    private static AsyncHttpClient asyncHttpClient;
    private static OFC_RestClient dmRestClient;
    private final Context context;

    public static OFC_RestClient getInstance(Context mContext) {
        if (dmRestClient == null) {
            dmRestClient = new OFC_RestClient(mContext);
        }
        return dmRestClient;
    }

    public OFC_RestClient(Context mContext) {
        this.context = mContext;
        if (asyncHttpClient == null) {
            asyncHttpClient = new AsyncHttpClient(true,80,443);
           // asyncHttpClient = new AsyncHttpClient();
        }
    }

    public static AsyncHttpClient getAsyncHttpClient(Context context) {
       //addHeadersToHttpClient(context);

        asyncHttpClient.setTimeout(TIME_OUT_FOR_SERVICE_REQUEST);

        return asyncHttpClient;
    }

    public void performHTTPGetRequest(final int requestCode, final String url, final RequestParams params, final OFC_NetworkCallback networkCallback) throws Exception {
        getAsyncHttpClient(context).get(context, url, params, new OFC_AsyncHttpResponseHandler(requestCode, networkCallback, url, params.toString(), true));
    }

    public void performPostRequest(final int requestCode, final String url, final Object reqObject,
                                   final OFC_NetworkCallback networkCallback) throws Exception {
        if (android.os.Build.VERSION.SDK_INT >= 19){
            getAsyncHttpClient(context).post(context, url, new StringEntity(reqObject.toString(), StandardCharsets.UTF_8), CONTENT_TYPE_APPLICATION_JSON, new OFC_AsyncHttpResponseHandler(requestCode, networkCallback, url, reqObject.toString(), false));

        } else{
            getAsyncHttpClient(context).post(context, url, new StringEntity(reqObject.toString(), HTTP.UTF_8), CONTENT_TYPE_APPLICATION_JSON, new OFC_AsyncHttpResponseHandler(requestCode, networkCallback, url, reqObject.toString(), false));

        }
        }


    public void performFormDataPostRequest(final int requestCode, final String url, Header[] headers,
                                           RequestParams params, final OFC_NetworkCallback networkCallback) throws Exception {
        getAsyncHttpClient(context).post(context, url, headers, params, "multipart/form-data", new OFC_AsyncHttpResponseHandler(requestCode, networkCallback, url, params, false));
    }



   private static void addHeadersToHttpClient(Context context) {
            try {
                String authKey = "";
                OFC_PrefsManager ofcPrefsManager = new OFC_PrefsManager(context);
                if (ofcPrefsManager.hasKey(Prefs_Keys.AUTH_KEY)) {
                    authKey = ofcPrefsManager.get(Prefs_Keys.AUTH_KEY);
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

}```



0

There are 0 best solutions below