2016-09-21 34 views
1

我只是試圖使用Retrofit來發送api調用。服務器正在響應正確的數據。我使用郵遞員(Chrome)進行了檢查。我的代碼如下SocketTimeOut使用改造時的異常

公共類MainActivity擴展活動器具retrofit2.Callback> {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    final OkHttpClient okHttpClient = new OkHttpClient.Builder() 
      .connectTimeout(6, TimeUnit.MINUTES) 
      .readTimeout(6, TimeUnit.MINUTES) 
      .writeTimeout(6, TimeUnit.MINUTES) 
      .build(); 


    Gson gson = new GsonBuilder() 
      .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") 
      .create(); 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("http://kokanplaces.com/") 
      .addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient) 
      .build(); 

    // prepare call in Retrofit 2.0 

    ApiInterface apiService = 
      ApiClient.getClient().create(ApiInterface.class); 

    Call<List<CityModel>> call = apiService.getCitiesList();; 
    //asynchronous call 
    call.enqueue(this); 
} 


@Override 
public void onResponse(Call<List<CityModel>> call, Response<List<CityModel>> response) { 
    int code = response.code(); 
    if (code == 200) { 
     for (CityModel cityModel : response.body()) { 
       System.out.println(
         cityModel.getCityname() + " (" + cityModel.getCityId() + ")"); 
      }   


    } else { 
     Toast.makeText(this, "Did not work: " + String.valueOf(code), Toast.LENGTH_LONG).show(); 
    } 

} 

@Override 
public void onFailure(Call<List<CityModel>> call, Throwable t) { 
    Toast.makeText(this, "failure", Toast.LENGTH_LONG).show(); 
    System.out.println(t.fillInStackTrace()); 
    t.printStackTrace(); 

} 

}

public interface ApiInterface { 
@POST("wp-json/getCities") 
Call<List<CityModel>> getCitiesList(); 

}

每當它是扔套接字超時例外。

任何解決方案將是很大的幫助。

回答

1

我遇到過像以前一樣的問題。我固定通過添加自定義OkHttpClient

Constants.TIMEOUT_CONNECTION = 60; 
private OkHttpClient getOkHttpClient() { 
     final OkHttpClient okHttpClient = new OkHttpClient.Builder() 
       .readTimeout(0, TimeUnit.NANOSECONDS) 
       .connectTimeout(Constants.TIMEOUT_CONNECTION, TimeUnit.SECONDS) 
       .writeTimeout(Constants.TIMEOUT_CONNECTION, TimeUnit.SECONDS) 
//    .sslSocketFactory(getSSLSocketFactory()) 
       .build(); 
     return okHttpClient; 
    } 

和retrofitAdapter:

retrofitAdapter = new Retrofit.Builder() 
         .baseUrl(ConstantApi.BASE_URL) 
         .addConverterFactory(GsonConverterFactory.create(gson)) 
         .client(getOkHttpClient()) 
         .build(); 

記住readTimeout是0,我使用改裝2.1.0。改造的默認超時時間爲10秒。我試圖設置readTimeout是60秒但沒有效果。

+0

我發現我的錯誤即 –

+0

而不是ApiInte rface apiService = ApiClient.getClient()。create(ApiInterface.class);我需要調用retrofit.create(ApiInterface.class);其他的事情都很好。 –

0

主題標籤:插座關閉套接字超時

說明:改造維護其鎖定套接字連接。

更多:https://github.com/square/okhttp/issues/3146

SOLUTION: 配置連接池喜歡下面的例子:

private OkHttpClient getOkHttpClient() { 
    final OkHttpClient okHttpClient = new OkHttpClient.Builder() 
      .connectionPool(new ConnectionPool(0,1,TimeUnit.NANOSECONDS)) 
      .build(); 
    return okHttpClient; 
} 

請記住,以紀念答案正確:)