我正在使用Retrofit 2.0,下面的代码将记录:
"insertListingImages, SocketOutOfTimeException:TimeOut timeout"
。
为什么会导致超时异常?我该如何解决?奇怪的是,如果改装电话hi()
不在改装电话中,insertListingImages()
一切都会正常工作。
retrofit = new Retrofit.Builder()
.baseUrl("http://IP_ADRESS/")
.addConverterFactory(GsonConverterFactory.create())
.build().create(DatabaseInterface.class);
retrofit.insertListingImages(imageListingRequest).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Log.d("insertListingImages", "Success");
retrofit.hi().enqueue(new Callback<Void>(){
@Override
public void onResponse(Call<Void> call, Response<Void> response){
Log.d("hiTest", "Success");
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.d("hiTest", "fail: " + t.toString() + " " + t.getMessage());
}
});
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.d("insertListingImages", "fail: " + t.toString() + " " + t.getMessage());
}
});
答案 0 :(得分:0)
您可以通过此代码更改连接超时和读取超时。您必须为改造设置自定义客户端
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS).addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.url)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
在上面的代码中将超时设置为120秒,您可以根据需要进行更改
需要在build.gradle文件中添加以下行
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'