Fast-android-networking: How to set timeout in Request

Created on 23 Sep 2016  路  20Comments  路  Source: amitshekhariitbhu/Fast-Android-Networking

ErrorCode=0,
ErrorMessage=com.androidnetworking.error.ANError: java.net.SocketTimeoutException

How I solve this problem, please?

question

Most helpful comment

@vsoft-phuong : You can pass custom okHttpClient with increased timeout.

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                . writeTimeout(120, TimeUnit.SECONDS)
                .build();

AndroidNetworking.initialize(getApplicationContext(),okHttpClient);  

All 20 comments

@vsoft-phuong : You can pass custom okHttpClient with increased timeout.

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                . writeTimeout(120, TimeUnit.SECONDS)
                .build();

AndroidNetworking.initialize(getApplicationContext(),okHttpClient);  

@vsoft-phuong : You can also customize it for a particular request.

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                . writeTimeout(120, TimeUnit.SECONDS)
                .build();
AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllUsers/{pageNumber}")
                 .addPathParameter("pageNumber", "0")
                 .addQueryParameter("limit", "3")
                 .addHeaders("token", "1234")
                 .setTag("test")
                 .setPriority(Priority.LOW)
                 .setOkHttpClient(okHttpClient) // passing a custom okHttpClient 
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                      // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error
                    }
                });

Thanks you!

Please post a tutorial on uploading multipart files in background service and update ui progress.

@hiteshrawat

AndroidNetworking.upload(url)
                 .addMultipartFile("image",file)    
                 .addMultipartParameter("key","value")
                 .build()
                 .setUploadProgressListener(new UploadProgressListener() {
                    @Override
                    public void onProgress(long bytesUploaded, long totalBytes) {
                      // do anything with progress 
                    }
                 })
                 .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                      // do anything with response                
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error 
                    }
                 }); 

This code does uploading in background you can write this anywhere.

Thanks Amit I have tried it.Its working fine. But I want implement it with the Service and make it work even after exiting the app.
I am beginner to android.Please guide me.

@hiteshrawat : Sure sure I am here to help everybody.
You can write the same code in your service and then use LocalBroadcast to post progress from service to Activity . Example is present here

Thanks Amit

How to get the status code for Response?

@nikhiljadhav159 : refer this.

@amitshekhariitbhu How can i send multiple image and video in single request?
is it like
while(loop){
.addMultipartFile("image[]",file[i])
}

i am little confuse

my problem is "com.androidnetworking.error.ANError"

only show this error

Any one solve this problem

@amitshekhariitbhu what is maximum value can we set in the connectTimeout, readTimeout and writeTimeout?

Depends on you

@amitshekhariitbhu is there any standard/best practices to follow it...

@vsoft-phuong : You can pass custom okHttpClient with increased timeout.

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                . writeTimeout(120, TimeUnit.SECONDS)
                .build();

AndroidNetworking.initialize(getApplicationContext(),okHttpClient);  

Does this code will work for Rx2AndroidNetworking???

@vsoft-phuong : You can pass custom okHttpClient with increased timeout.

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                . writeTimeout(120, TimeUnit.SECONDS)
                .build();

AndroidNetworking.initialize(getApplicationContext(),okHttpClient);  

Does this code will work for Rx2AndroidNetworking???

Will this code work for all network calls used in the android project?? If the above is written in the Application class.

not working.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Patrick-Wallin picture Patrick-Wallin  路  8Comments

MahdiPishguy picture MahdiPishguy  路  5Comments

MohadFarhan picture MohadFarhan  路  5Comments

stridercheng picture stridercheng  路  6Comments

GulajavaMinistudio picture GulajavaMinistudio  路  3Comments