-1

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

davidism
  • 121,510
  • 29
  • 395
  • 339
  • 1
    You can configure timeouts on your `OkHttpClient.Builder`: https://square.github.io/okhttp/recipes/#timeouts-kt-java – CommonsWare Mar 28 '21 at 18:41
  • Does this answer your question? [How to set connection timeout with OkHttp](https://stackoverflow.com/questions/25953819/how-to-set-connection-timeout-with-okhttp) – a_local_nobody Mar 28 '21 at 20:17

1 Answers1

1

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)
Yunis Rasulzade
  • 325
  • 3
  • 12