J'ai un problème avec Retrofit sur mon émulateur exécutant Android 4.3 et mon appareil est allumé Android 4.4.2 alors que le même code s'exécute normalement sur un autre émulateur avec = Android 7.1.1
Chaque fois que j'essaie d'exécuter la demande get
, j'obtiens une exception de délai d'expiration.
Java.net.SocketTimeoutException: failed to connect to jsonplaceholder.typicode.com/2606:4700:30::681c:3f5 (port 443) after 10000ms
at libcore.io.IoBridge.connectErrno(IoBridge.Java:159)
at libcore.io.IoBridge.connect(IoBridge.Java:112)
at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:192)
at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:459)
at Java.net.Socket.connect(Socket.Java:842)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.Java:73)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.Java:246)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.Java:166)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.Java:257)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.Java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.Java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.Java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.Java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.Java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.Java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.Java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.Java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.Java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.Java:32)
at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1080)
at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:573)
at Java.lang.Thread.run(Thread.Java:841)
Le code est ci-dessous
public interface Api {
String BASE_URL = "https://jsonplaceholder.typicode.com/";
@GET("posts")
Call<ArrayList<Post>> getPosts();
}
et l'appel à l'api
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<ArrayList<Post>> call = api.getPostes();
Log.i("RequestUrl", call.request().url().toString());
call.enqueue(new Callback<ArrayList<Post>>() {
@Override
public void onResponse(Call<ArrayList<Post>> call, Response<ArrayList<Post>> response) {
mPostsList.setValue(response.body());
}
@Override
public void onFailure(Call<ArrayList<Post>> call, Throwable t) {
Log.e("Posts", "Error occurred", t);
}
});
Android avant 21 a un SSL manquant et Retrofit ne fonctionnera pas. En utilisant les services Google, vous pouvez mettre à jour les protocoles de l'appareil après que la demande HTTP fonctionnera
//compile 'com.google.Android.gms:play-services-base:11.0.0'
//remember to add the library in your dependencies
//compile 'com.google.Android.gms:play-services-base:$currentVersion'
ProviderInstaller.installIfNeededAsync(this, new ProviderInstallListener() {
@Override
public void onProviderInstalled() {
//Do your http request here
}
@Override
public void onProviderInstallFailed(int errorCode, Intent recoveryIntent) {
//sad face :C is sad
}
});