2016-09-21 6 views
1

Ich versuche nur, Post-API-Anruf mit Retrofit zu tun.Der Server reagiert mit korrekten Daten.Ich habe mit Postman (Chrome) überprüft. Mein Code ist wie folgtSocketTimeOut Ausnahme bei der Verwendung von Retrofit

public class MainActivity erweitert Aktivität implementiert 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(); 

}

Jedes Mal, es wirft Socket-Timeout Ausnahme .

Jede Lösung wird eine große Hilfe sein.

Antwort

1

Ich habe die Probleme wie Sie zuvor getroffen. Ich reparierte durch individuelle Zugabe 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; 
    } 

und retrofitAdapter:

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

Denken Sie daran, readTimeout 0 ist, ich bin mit Retrofit 2.1.0. Das Standard-Timeout für die Nachrüstung beträgt 10 Sekunden. Ich habe versucht, readTimeout ist 60 Sekunden aber keine Wirkung einzustellen.

+0

ich meinen Fehler gefunden zu markieren dass –

+0

anstelle von ApiInte rface apiService = ApiClient.getClient(). create (ApiInterface.class); Ich muss umrüsten.create (ApiInterface.class) aufrufen; Andere Dinge waren in Ordnung. –

0

Topic tags: Socket geschlossen, socket Timeout

Erläuterung: Retrofit Verbindung hält die Buchse blockiert.

Mehr:https://github.com/square/okhttp/issues/3146

LÖSUNG: configure Connection wie in Beispiel unten:

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

Bitte denken Sie daran Antwort als richtig :)

Verwandte Themen