2017-09-05 21 views
0

我使用的是改造2.0,下面的代碼將記錄:(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

您可以通過此代碼更改連接超時和讀取超時。你必須設置自定義客戶端的改造

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秒,你可以根據你的需要

需要添加下面一行在構建改變.gradle文件

compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' 
+0

我會試試這個,當我cnan;但爲什麼會發生超時? –

+0

閱讀此https://stackoverflow.com/questions/30767460/okhttp-retrofit-default-timeout – Nithinlal

相關問題