Android app works on android 8 but crashes on android 6 - android

I am building an app and it is working fine in Android 8
when I test it on older version I get the following crash
Android Version: 6.0.1
Device: samsung (SM-J500H)
Crash log
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:357)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:319)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:283)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:168)
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 com.sbs16.ensofia.network.manager.EnsofiaHttpLoggingInterceptor.intercept(EnsofiaHttpLoggingInterceptor.kt:46)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.sbs16.ensofia.network.manager.EnsofiaResponseInterceptor.intercept(EnsofiaResponseInterceptor.kt:7)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.sbs16.ensofia.network.manager.AuthorizationInterceptor.intercept(AuthorizationInterceptor.kt:61)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.sbs16.ensofia.network.manager.ConnectivityInterceptor.intercept(ConnectivityInterceptor.kt:16)
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:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:337)
at com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:231)
at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:115)
at com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:643)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 35 more
Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
... 41 more
can anyone help please?

If you use a self-signed certificate, Google don't accept this. You should use X.509.
When you request your server, first call the trustCerts method as defined below. Then, you can initiate a request.
#SuppressLint("TrulyRandom")
public static void trustCerts() {
try {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
#Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
#Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
#Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
} catch (Exception e) {
Log.e(TAG, "Could not trust certs:", e);
}
}

Related

BroadcastReceiver + Retrofit NetworkOnMainThreadException if app killed

Logic steps are in my sample:
App starts
Create alarmanager with 10 secs delay
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000L, getIntent());
private PendingIntent getIntent() {
return PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), FLAG_UPDATE_CURRENT);
}
MyReceiver:
#Override
public void onReceive(final Context context, Intent intent) {
updateManager.checkUpdate()
}
UpdateManager:
public void checkVersion() {
versionManager.checkUpdate()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(versionResponse -> {
Log.d(TAG, "__result: " + versionResponse.getStatus());
}, throwable -> Log.d("TAG", "_err: " + throwable.toString()));
}
where versionManager.checkUpdate() a retforit call, return with
a Observable.
If app is running then I'm getting result, but If i kill it before onReceive method called then I got NetworkOnMainThreadException exception.
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
at java.net.InetAddress.getAllByName(InetAddress.java:1154)
at okhttp3.Dns$1.lookup(Dns.java:40)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:214)
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.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:213)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
at io.reactivex.Observable.subscribe(Observable.java:10700)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:10700)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.subscribeInner(ObservableFlatMap.java:162)
at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(ObservableFlatMap.java:139)
at io.reactivex.internal.operators.single.SingleToObservable$SingleToObservableObserver.onSuccess(SingleToObservable.java:59)
at io.reactivex.internal.operators.single.SingleMap$MapSingleObserver.onSuccess(SingleMap.java:63)
at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback$FlatMapSingleObserver.onSuccess(SingleFlatMap.java:111)
at io.reactivex.internal.operators.single.SingleJust.subscribeActual(SingleJust.java:30)
at io.reactivex.Single.subscribe(Single.java:2692)
at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback.onSuccess(SingleFlatMap.java:84)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
Why?
Thx!

API call on Android 19 emulator : isConnected failed: EHOSTUNREACH (No route to host)

I'm retrieving data from an API. When I try to launch the app on an Android emulator running Android API 19 the request fails with the error below.
It work perfectly fine using other versions of the emulator (like Android API 27).
I changed the URL to target another API from a previous project and it works. So it seems the issue is with this specific API but I don't understand why, especially as when I pass the URL into the emulator's browser it works fine.
I've seen some people suggesting this is an issue with the computer's firewall, but there is no firewall enabled on mine.
Retrofit
interface SpaceXApi {
#GET("rockets")
fun getRockets(): Observable<MutableList<RocketDto>>
}
object SpaceXApiConstants {
const val BASE_URL = "https://api.spacexdata.com/v3/"
}
// Interceptor passed to OkHttpClient Builder
class ConnectivityInterceptor(private val context: Context) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
if (isConnected()) {
return chain.proceed(chain.request())
} else {
throw NoNetworkException()
}
}
}
Error
D/OkHttp: --> GET https://api.spacexdata.com/v3/rockets
D/OkHttp: <-- HTTP FAILED: java.net.ConnectException: Failed to connect to api.spacexdata.com/2606:4700:30::681f:5749:443
W/System.err: java.net.ConnectException: Failed to connect to api.spacexdata.com/2606:4700:30::681f:5749:443
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:165)
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.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:213)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at com.example.myproject.network.ConnectivityInterceptor.intercept(ConnectivityInterceptor.kt:13)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:42)
at io.reactivex.Observable.subscribe(Observable.java:12090)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:12090)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.net.ConnectException: failed to connect to api.spacexdata.com/2606:4700:30::681f:5749 (port 443) after 20000ms: isConnected failed: EHOSTUNREACH (No route to host)
at libcore.io.IoBridge.isConnected(IoBridge.java:223)
at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
12-02 17:44:40.445 4877-4877/com.example.myproject W/System.err: 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:843)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:73)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:245)
... 38 more
Caused by: libcore.io.ErrnoException: isConnected failed: EHOSTUNREACH (No route to host)
at libcore.io.IoBridge.isConnected(IoBridge.java:208)
... 45 more
Any solution?
Looks like a protocol issue : devices with API 19 and older don't use TLS 1.2 by default.
You can enable it with this :
ProviderInstaller.installIfNeededAsync(this, new ProviderInstaller.ProviderInstallListener() {
#Override
public void onProviderInstalled() {
}
#Override
public void onProviderInstallFailed(int i, Intent intent) {
Log.i(TAG, "Provider install failed (" + i + ") : SSL Problems may occurs");
}
});
This has to be called before your first call, so I usually call it in the OnCreate of the Application object.
For more information, you can check this link : https://quizlet.com/blog/working-with-tls-1-2-on-android-4-4-and-lower

HttpURLConnection in Android working fine, but OkHttp gives "Network is unreachable" on Kitkat

I have a problem with this particular site: https://tastedive.com/read/api
If I do an HTTP request with HttpURLConnection, I get a normal HTML response (on Android this code needs to be in a separate thread and also put all the necessary tries and catches):
StringBuilder result = new StringBuilder();
URL url = new URL("https://tastedive.com/read/api");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
System.out.println(result.toString()); // shows normal HTML response
But if I do it with OkHttp, with this code...
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://tastedive.com/read/api")
.build();
client.newCall(request).enqueue(new okhttp3.Callback() {
#Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.d("MY", "failure", e);
}
#Override
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
System.out.println(response.body().string());
}
}
});
...I get this error in onFailure:
java.net.ConnectException: Failed to connect to tastedive.com/2606:4700:30::681c:3a5:443
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:242)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
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:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.net.ConnectException: failed to connect to tastedive.com/2606:4700:30::681c:3a5 (port 443) after 10000ms: isConnected failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.isConnected(IoBridge.java:223)
at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
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:843)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:71)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160) 
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:200) 
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147) 
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 
Caused by: libcore.io.ErrnoException: isConnected failed: ENETUNREACH (Network is unreachable)
at libcore.io.IoBridge.isConnected(IoBridge.java:208)
at libcore.io.IoBridge.connectErrno(IoBridge.java:161) 
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:843) 
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:71) 
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240) 
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160) 
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:200) 
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147) 
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:841) 
This OkHttp that I'm using is from Retrofit 2.4.0.
Happens on both the emulator and the real device, but only on Kitkat. Also note that the exception is thrown immediately, not only after 10000 ms.
I solved it! The real device was actually getting Javax.net.ssl.SSLHandshakeException, while the emulator was getting java.net.ConnectException. This StackOverflow discussion describes that there's a bug around TLS for pre-Lollipop devices.
Javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: Failure in SSL library, usually a protocol error
So now I use this code from this answer: https://stackoverflow.com/a/50640113/9702500, it solves both the emulator exception and the device exception:
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
.supportsTlsExtensions(true)
.tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
.build();
OkHttpClient client = new OkHttpClient.Builder()
.connectionSpecs(Collections.singletonList(spec))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://tastedive.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();

Failure response retrofit 2

I Implemented sample retrofit 2 response via GET type, where I click button to get the response, but I have Failure response
public interface GithubServise {
#GET("/users/waadalkatheri/repos")
Call<ResponseBody> getGithub();
}
public void loadData (View view){
String LINK = "https://api.github.com";
Retrofit retrofit = new Retrofit.Builder ().baseUrl (LINK).build ();
GithubServise githubServise= retrofit.create (GithubServise.class);
githubServise.getGithub ().enqueue (new Callback<ResponseBody> () {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
textView.setText (response.body ().string ());
Log.v ("TAG","yes");
} catch (IOException e) {
e.printStackTrace ();
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
textView.setText ("no data");
}
});
}
Stacktrace
06-25 10:13:00.870 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: javax.net.ssl.SSLHandshakeException:
javax.net.ssl.SSLProtocolException: SSL handshake aborted:
ssl=0xb95dcb40: Failure in SSL library, usually a protocol error 06-25
10:13:00.874 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1
alert protocol version (external/openssl/ssl/s23_clnt.c:741
0x9dc27d4d:0x00000000) 06-25 10:13:00.886
2249-2249/com.example.waadalkatheri.retrofittraning W/System.err:
at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
06-25 10:13:00.898 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
06-25 10:13:00.902 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
06-25 10:13:00.910 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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)
06-25 10:13:00.926 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
06-25 10:13:00.930 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
06-25 10:13:00.934 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
06-25 10:13:00.938 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147) 06-25 10:13:00.942 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 06-25
10:13:00.946 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856) 06-25 10:13:00.950 2249-2249/com.example.waadalkatheri.retrofittraning W/System.err:
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted:
ssl=0xb95dcb40: Failure in SSL library, usually a protocol error 06-25
10:13:00.958 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1
alert protocol version (external/openssl/ssl/s23_clnt.c:741
0x9dc27d4d:0x00000000)
at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native
Method) 06-25 10:13:00.962
2249-2249/com.example.waadalkatheri.retrofittraning W/System.err:
at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
... 23 more 06-25 10:13:00.974 2249-2249/com.example.waadalkatheri.retrofittraning V/TAG: no data
06-25 10:13:00.978 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: javax.net.ssl.SSLHandshakeException:
javax.net.ssl.SSLProtocolException: SSL handshake aborted:
ssl=0xb95fea28: Failure in SSL library, usually a protocol error 06-25
10:13:00.986 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1
alert protocol version (external/openssl/ssl/s23_clnt.c:741
0x9dc27d4d:0x00000000) 06-25 10:13:01.002
2249-2249/com.example.waadalkatheri.retrofittraning W/System.err:
at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
06-25 10:13:01.006 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
06-25 10:13:01.010 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
06-25 10:13:01.014 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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)
06-25 10:13:01.018 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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) 06-25 10:13:01.022 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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)
06-25 10:13:01.026 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147) 06-25 10:13:01.030 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb95fea28: Failure in SSL library, usually a protocol
error
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version (external/openssl/ssl/s23_clnt.c:741
0x9dc27d4d:0x00000000)
at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native
Method)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
06-25 10:13:01.034 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: ... 23 more 06-25 10:13:02.586
2249-2249/com.example.waadalkatheri.retrofittraning V/TAG: no data
06-25 10:13:02.586 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: javax.net.ssl.SSLHandshakeException:
javax.net.ssl.SSLProtocolException: SSL handshake aborted:
ssl=0xb95dcb40: Failure in SSL library, usually a protocol error 06-25
10:13:02.590 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1
alert protocol version (external/openssl/ssl/s23_clnt.c:741
0x9dc27d4d:0x00000000) 06-25 10:13:02.594
2249-2249/com.example.waadalkatheri.retrofittraning W/System.err:
at
org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:436)
06-25 10:13:02.598 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
06-25 10:13:02.602 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
06-25 10:13:02.606 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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)
06-25 10:13:02.610 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: 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)
06-25 10:13:02.614 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 06-25 10:13:02.618 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
06-25 10:13:02.622 2249-2249/com.example.waadalkatheri.retrofittraning
W/System.err: at
okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856) 06-25 10:13:02.626 2249-2249/com.example.waadalkatheri.retrofittraning W/System.err:
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted:
ssl=0xb95dcb40: Failure in SSL library, usually a protocol error
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version (external/openssl/ssl/s23_clnt.c:741
0x9dc27d4d:0x00000000)
at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native
Method)
at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:395)
... 23 more
As seen here, your api https://api.github.com supports only TLS 1.2. Your test device might have an android version 16+ and TLS 1.2 is supported in API level 16+, but are not enabled by default.For 20+ devices it is enabled by default. So for 16<device<20 create a custom SSLSocketFactory with TLS 1.2 enabled like below
TLSSocketFactory.java
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory delegate;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
delegate = context.getSocketFactory();
}
#Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}
#Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}
#Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(delegate.createSocket());
}
#Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
}
#Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}
#Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
}
#Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}
#Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.2"});
}
return socket;
}
}
Now create an OkHttpClient and add the client to retrofit like this
public void loadData (View view){
OkHttpClient client=new OkHttpClient();
try {
client = new OkHttpClient.Builder()
.sslSocketFactory(new TLSSocketFactory())
.build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String LINK = "https://api.github.com";
Retrofit retrofit = new Retrofit.Builder ().client(client).baseUrl (LINK).build ();
GithubServise githubServise= retrofit.create (GithubServise.class);
githubServise.getGithub ().enqueue (new Callback<ResponseBody> () {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
textView.setText (response.body ().string ());
Log.v ("TAG","yes");
} catch (IOException e) {
e.printStackTrace ();
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
textView.setText ("no data");
}
});
}
When onFailure gets called it means that the error was in the networking. So something in the conection is not working, maybe you have already checked these things..
Make sure you have internet conection and the url is correct by doing the same http request from your browser on your mobile (Chrome).
Check that in the manifest you have asked for the Internet permission:
uses-permission android:name="android.permission.INTERNET"

okhttp, unexpected end of stream on Connection

I'm currently learning android, and i encountered some problem with okhttp3.
Here's the stacktrace.
07-31 17:57:54.933 23100-24099/com.ullxfeg.networktest W/System.err: java.io.IOException: unexpected end of stream on Connection{10.0.2.2:8099, proxy=DIRECT hostAddress=/10.0.2.2:8099 cipherSuite=none protocol=http/1.1}
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:208)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
07-31 17:57:54.934 23100-24099/com.ullxfeg.networktest W/System.err: 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)
07-31 17:57:54.935 23100-24099/com.ullxfeg.networktest W/System.err: 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:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at com.ullxfeg.networktest.MainActivity$1.run(MainActivity.java:60)
07-31 17:57:54.936 23100-24099/com.ullxfeg.networktest W/System.err: at java.lang.Thread.run(Thread.java:764)
Caused by: java.io.EOFException: \n not found: limit=0 content=…
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:237)
at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
... 17 more
My working environment is apache 2.4.34, Windows 10, Android Studio 3.1.3 and minSdkVersion 22. OKHTTP version is 3.11.0
My sample code is:
private void sendRequestWithOKHttp() {
new Thread(new Runnable() {
#Override
public void run() {
try {
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
Request request = new Request.Builder()
.url("http://10.0.2.2:8099/get_data.xml")
.addHeader("Connection", "close")
.get()
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseXMLWithPull(responseData);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
I first accidentally fixed my problem with apache httpd.conf file:
# Various default settings
Include conf/extra/httpd-default.conf
# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
#Include conf/extra/httpd-proxy-html.conf
</IfModule>
but later this problem occurs every time and i don't know what's going on.
Is there any solutions?

Categories

Resources