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.
ich meinen Fehler gefunden zu markieren dass –
anstelle von ApiInte rface apiService = ApiClient.getClient(). create (ApiInterface.class); Ich muss umrüsten.create (ApiInterface.class) aufrufen; Andere Dinge waren in Ordnung. –