Android 4.3を実行しているエミュレータでRetrofitに問題があり、デバイスがAndroid 4.4.2にある場合、同じコードが= Android 7.1.1
get
リクエストを実行しようとするたびに、タイムアウト例外が発生します。
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)
コードは以下です
public interface Api {
String BASE_URL = "https://jsonplaceholder.typicode.com/";
@GET("posts")
Call<ArrayList<Post>> getPosts();
}
そして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);
}
});
21より前のAndroidにはSSLが欠落していて、レトロフィットが機能しません。 Googleサービスを使用すると、HTTPリクエストが機能するようになった後にデバイスプロトコルを更新できます
//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
}
});