我已在我的项目中实施了robospice并对所有api调用使用了改造。对于某些请求,我需要增加超时,请让我知道我该怎么做?
实际上我正在使用扩展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;
}
}
答案 0 :(得分:17)
// 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
,具体取决于构建应用的风格。
最后,这些是使一切运转的主要依赖关系:
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"
...
}
答案 1 :(得分:5)
在RetrofitSpiceService
扩展服务中,您需要覆盖createRestAdapterBuilder()
。
@Override
protected Builder createRestAdapterBuilder() {
Builder builder = super.createRestAdapterBuilder();
builder.setClient(new CustomClient());
return builder;
}
CustomClient
是您需要自己编写以提供自己的超时的课程。如果您正在使用OkHttp,请查看默认OkClient
以供参考:https://github.com/square/retrofit/blob/43b7ea14e5aca1d710deccb95b79484b03e99bb9/retrofit/src/main/java/retrofit/client/OkClient.java
答案 2 :(得分:3)
我不了解robospice但是通过改造2+和okhttp3 +你应该设置一个像这样的新客户。
OkHttpClient okHttpClient = new OkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constantes.URL_DOMAIN)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient.newBuilder().connectTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).readTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).writeTimeout(Constantes.TIMEOUT, TimeUnit.SECONDS).build())
.build();
在你的模块build.gradle中导入它:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'