如何增加超时在robospice Android的改造请求? [英] How to increase timeout for retrofit requests in robospice android?

查看:255
本文介绍了如何增加超时在robospice Android的改造请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现robospice在我的项目,并利用改造的所有API调用。对于一些要求,我需要增加超时,请让我知道我该怎么办呢?

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?

其实我使用的扩展RetrofitGsonSpiceService服务。 我服务类的code是如下:

Actually I am using service that extends RetrofitGsonSpiceService. The code of my service class is below:

公共类的MyService扩展RetrofitGsonSpiceService {

public class MyService extends RetrofitGsonSpiceService {

@Override
public void onCreate() {
    super.onCreate();
    addRetrofitInterface(MyInterface.class);
}

@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
    CacheManager cacheManager = new CacheManager();
    ObjectPersisterFactory persistFactory = new RetrofitObjectPersisterFactory(application,
            getConverter(), getCacheFolder());

    persistFactory.setAsyncSaveEnabled(true);
    cacheManager.addPersister(persistFactory);

    return cacheManager;
}

@Override
protected String getServerUrl() {
    return Utils.getBaseUrl();
}

@Override
protected RestAdapter.Builder createRestAdapterBuilder() {
    RestAdapter.Builder builder = new RestAdapter.Builder().setRequestInterceptor(
            new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader(Const.HEADER_DEVICE_TYPE_KEY,
                            Const.HEADER_DEVICE_TYPE_VALUE);
                }
            }
    );
    builder.setEndpoint(getServerUrl()).setConverter(getConverter());
    return builder;
}

}

推荐答案

我偶然在这里有一个类似的问题,并最终找到了答案其他地方。假如有过一个更完整的例子在这里,我也存了一些时间,所以我盘旋回发布的内容只是在情况下,它可以帮助别人为我工作:

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:

    // 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();

注意:对于我们来说, getBaseApiUrl()返回类似: https://some.company.com / API /
getRetrofitLogLevel()返回无论是 RestAdapter.LogLevel.FULL RestAdapter.LogLevel.NONE 这取决于应用程序的味道建造的。

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:

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"
    ...
}

这篇关于如何增加超时在robospice Android的改造请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆