How to increase timeout for retrofit requests in robospice android?

前端 未结 3 815
刺人心
刺人心 2021-02-07 19:02

I have implemented robospice in my project and using retrofit for all api calls. For some of requests, I need to increase timeout, please let me know how can I do that?

3条回答
  •  囚心锁ツ
    2021-02-07 19:37

    I stumbled in here with a similar question and eventually found the answer elsewhere. Had there been a more complete example here, I would have saved some time so I circled back to post what worked for me just in case it helps others:

    Adapter with increased read timeout

        // create client
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(60 * 1000, TimeUnit.MILLISECONDS);
    
        // create rest adapter using the client above
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(getBaseApiUrl())
                .setClient(new OkClient(okHttpClient))
                // this gson converter below is optional. We use it for parsing dates and enums
                .setConverter(new GsonConverter(createApiGson()))
                .setLogLevel(getRetrofitLogLevel())
                .build();
    

    NOTE: for us, getBaseApiUrl() returns something like: https://some.company.com/api/
    and getRetrofitLogLevel() returns either RestAdapter.LogLevel.FULL or RestAdapter.LogLevel.NONE depending on which flavor of the app is built.

    Lastly, these are the main dependencies that make everything work:

    Key dependencies

    dependencies {
        ...
        compile "com.squareup.retrofit:retrofit:1.5.0"
        compile "com.squareup.retrofit:retrofit-mock:1.5.0"
        compile "com.squareup.okhttp:okhttp:1.5.4"
        compile "com.google.code.gson:gson:2.2.4"
        ...
    }
    

提交回复
热议问题