简体   繁体   中英

How to increase the timeout of a request in OkHttp in android?

I am trying to make an app which will connect to a flask server. I am using the OkHttp library for sending and receiving data. My problem is I want to run a python script which will take at least 2 mins to complete and then send a "success" message after completion on a button click. So whenever I send a request to the server, I always get a timeout message in my android logcat. So I would like to know if there is any method to increase the timeout.

Here is my onResponse function:

public void onResponse(Call call, final Response response) throws IOException {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String loginResponseString = response.body().string().trim();
                        Log.d("LOGIN", "Response from the server : " + loginResponseString);
                        if (loginResponseString.equals("success")) {
                            Log.d("Artist", "Artist Found");
                            
                        } else if (loginResponseString.equals("failure")) {
                            Log.d("Artist", "Artist Not Found");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(Artist.this, "Something went wrong. Please try again later.", Toast.LENGTH_SHORT).show();
                    }
                }
            });

Whenever I give a request, the code always executes the catch part.

Android Logcat:

D/FAIL: timeout

PS. I'm a beginner and I don't know much about flask and android development

This is a sample for 40 seconds. You can modify it to 120 seconds

OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .addInterceptor(new HttpInterceptor())
                .connectTimeout(40000, TimeUnit.MILLISECONDS)
                .readTimeout(40000, TimeUnit.MILLISECONDS)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM