App crash always on this point if there is no internet or return null.
Following is log
FATAL EXCEPTION: OkHttp Dispatcher
Process: , PID: 24400
java.lang.NullPointerException: interceptor
RestClient$1#301dd345 returned null
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:157)
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:818)
Here is Log
05-01 09:17:37.920 24400-24439/? D/OkHttp: --> END GET
05-01 09:17:37.922 24400-24439/? D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "": No address associated with hostname
05-01 09:17:37.923 24400-24439/? W/System.err: java.net.UnknownHostException: Unable to resolve host "": No address associated with hostname
05-01 09:17:37.923 24400-24439/? W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:427)
05-01 09:17:37.924 24400-24439/? W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
05-01 09:17:37.924 24400-24439/? W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
05-01 09:17:37.924 24400-24439/? W/System.err: at okhttp3.Dns$1.lookup(Dns.java:39)
05-01 09:17:37.924 24400-24439/? W/System.err: at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
05-01 09:17:37.924 24400-24439/? W/System.err: at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
05-01 09:17:37.925 24400-24439/? W/System.err: at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
05-01 09:17:37.925 24400-24439/? W/System.err: at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:213)
05-01 09:17:37.927 24400-24439/? W/System.err: at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
05-01 09:17:37.927 24400-24439/? W/System.err: at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
05-01 09:17:37.927 24400-24439/? W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
05-01 09:17:37.927 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.927 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.928 24400-24439/? W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
05-01 09:17:37.928 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.928 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.928 24400-24439/? W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
05-01 09:17:37.928 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.928 24400-24439/? W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.929 24400-24439/? W/System.err: at RestClient$1.intercept(RestClient.java:66)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
05-01 09:17:37.929 24400-24439/? W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
05-01 09:17:37.930 24400-24439/? W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
05-01 09:17:37.930 24400-24439/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-01 09:17:37.930 24400-24439/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-01 09:17:37.930 24400-24439/? W/System.err: at java.lang.Thread.run(Thread.java:818)
Its my Network class Code. Here i am making request. java.lang.NullPointerException: interceptor RestClient$1#301dd345 returned null
Another log show in same sequence show this message
D/OkHttp: <-- HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "example.com": No address associated with hostname
public static String API_BASE_URL = "";
public static final String BASE_URL_QA = "https://example.com";
public static final String BASE_URL_LIVE = "https://example.com";
private Retrofit builder;
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private Response response;
public RestClient() {
if (SharedPreferenceManager.getSharedInstance().getStringFromPreferances(Constants.SharedPreferenceKeys.QA.getKey()).equals("yes")) {
BASE_URL = BASE_URL_QA;
} else {
BASE_URL = BASE_URL_LIVE;
}
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
if (!AppData.isTrue) {
httpClient.interceptors().add(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
try {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder();
if (AppData.isBearer) {
String token = getAccessToken();
String bearer = getToken_type();
if (AppUtils.getAppUtils(null).isNullOrEmpty(token) && SharedPreferenceManager.getSharedInstance().getFromPreferences() != null) {
token = SharedPreferenceManager.getToken();
bearer = SharedPreferenceManager.getTokentype();
}
requestBuilder.addHeader("Authorization", bearer + " " + token);
}
requestBuilder.addHeader("key", "");
Request request = requestBuilder.build();
AppData.isTrue = false;
response = chain.proceed(request);
} catch (Exception e) {
e.printStackTrace();
} catch (Error e2) {
Log.e("", "ROME parse error2: " + e2.toString());
}
return response;
}
});
}
httpClient.addInterceptor(logging);
OkHttpClient shortHttpClient = httpClient.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
builder = new Retrofit.Builder().
baseUrl(BASE_URL).
addConverterFactory(new ToStringConverterFactory()).
addConverterFactory(GsonConverterFactory.create()).
addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
client(shortHttpClient).
build();
}
public <S> S createService(Class<S> serviceClass) {
return builder.create(serviceClass);
}
Related
Two possibilities : either i pass a header with "Authorization: APIKEY" but it says that i don't have access (code 401) or i pass "Authorization: Bearer APIKEY" and it throws an exception. After looking up on SO i found the solution to add interceptor, also to add a header with "Connection: close" but still, i don't receive the data.
Here's my code :
public class RetrofitYouSign {
private static Retrofit sRetrofit;
private static final String URL = "https://staging-api.yousign.com/";
private static OkHttpClient sOkHttpClient = new OkHttpClient.Builder()
.addInterceptor(
new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer MY-API-KEY")
.addHeader("Content-Type", "application/json")
.addHeader("Connection", "close")
.build();
return chain.proceed(request);
}
}).build();
public static Retrofit getRetrofit(){
if (sRetrofit == null) {
sRetrofit = new Retrofit.Builder()
.client(sOkHttpClient)
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return sRetrofit;
}
public interface GetYouSign {
#GET("users/")
Call<Yousign> getYouSignData();
}
}```
And here is my stack if i enter with the name Bearer :
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup I/System.out: [okhttp3.internal.http.StatusLine.parse(StatusLine.java:69), okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189), okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), com.jmjsolution.solarpro.services.retrofitClient.RetrofitYouSign$1.intercept(RetrofitYouSign.java:31), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147), okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121), okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200), okhttp3.RealCall$AsyncCall.execute(RealCall.java:147), okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32), java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162), java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636), java.lang.Thread.run(Thread.java:764)]
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err: java.net.ProtocolException: Unexpected status line: ��
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.StatusLine.parse(StatusLine.java:69)
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
2019-04-11 19:25:12.894 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.895 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
2019-04-11 19:25:12.896 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.896 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
2019-04-11 19:25:12.897 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.897 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.897 1492-1492/com.jmjsolution.solarup W/System.err: at com.jmjsolution.solarpro.services.retrofitClient.RetrofitYouSign$1.intercept(RetrofitYouSign.java:31)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2019-04-11 19:25:12.898 1492-1492/com.jmjsolution.solarup W/System.err: at java.lang.Thread.run(Thread.java:764)
Otherwise i receiver a code 401, if i don't put the word Bearer.
I tried the code with the interceptor (as you can see in my code) but nothing seems to work. Any solution would be really appreciated, thank you.
try this int APiInterface class:-
#GET("users")
Call<Yousign> getYouSignData(#Header("Authorization") String token);
or in your activity like this :-
getYouSignData("Bearer your api key")
You can simply pass you (bearer + auth token) as Authorization parameter like below:
#GET("api/GetProfile)
Call<UserProfile> getProfile(#Header("Authorization") String authHeader);
then call like this
Call<UserProfile> calltargetResponce = client.getProfile("Bearer "+token);
App crashes on this line if response won't come.
chain.proceed(requestBuilder.build())
Here is my class for RetrofitClient
class RetrofitClient {
private val apiService: ApiServiceInterface
init {
val builder = Retrofit.Builder()
builder.baseUrl(RequestParameters.BASE_URL)
builder.addConverterFactory(GsonConverterFactory.create())
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//added for adapter
builder.client(getClient())
val retrofit = builder.build()
apiService = retrofit.create(ApiServiceInterface::class.java)
}
companion object {
private var clientInstance: RetrofitClient? = null
lateinit var context: Context
fun getInstance(): RetrofitClient {
if (clientInstance == null) {
clientInstance = RetrofitClient()
}
return clientInstance as RetrofitClient
}
}
fun getApiInterface(): ApiServiceInterface {
return apiService
}
private fun getClient(): OkHttpClient {
val httpClient = OkHttpClient.Builder()
httpClient.readTimeout(25, TimeUnit.SECONDS)
httpClient.connectTimeout(25, TimeUnit.SECONDS)
httpClient.addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
.header(RequestParameters.X_API, RequestParameters.H_XAPI_KEY)
.method(original.method(), original.body())
chain.proceed(requestBuilder.build())
}
// set your desired log level
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.HEADERS
logging.level = HttpLoggingInterceptor.Level.BODY
httpClient.addInterceptor(logging)
return httpClient.build()
}
}
Here is the error in log:
1542609097.397 26572-26572/com.app.mylife W/System.err: java.net.SocketTimeoutException: timeout
1542609097.397 26572-26572/com.app.mylife W/System.err: at okio.Okio$4.newTimeoutException(Okio.java:230)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okio.RealBufferedSource.indexOf(RealBufferedSource.java:345)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:217)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:212)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.398 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.398 26572-26572/com.app.mylife W/System.err: at com.app.mylife.retrofit.RetrofitClient$getClient$1.intercept(RetrofitClient.kt:64)
1542609097.399 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
1542609097.399 26572-26572/com.app.mylife W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
1542609097.399 26572-26572/com.app.mylife W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
1542609097.399 26572-26572/com.app.mylife W/System.err: at okhttp3.RealCall.execute(RealCall.java:77)
1542609097.399 26572-26572/com.app.mylife W/System.err: at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
1542609097.399 26572-26572/com.app.mylife W/System.err: at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
1542609097.399 26572-26572/com.app.mylife W/System.err: at io.reactivex.Observable.subscribe(Observable.java:10179)
1542609097.399 26572-26572/com.app.mylife W/System.err: at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
1542609097.399 26572-26572/com.app.mylife W/System.err: at io.reactivex.Observable.subscribe(Observable.java:10179)
1542609097.399 26572-26572/com.app.mylife W/System.err: at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
1542609097.399 26572-26572/com.app.mylife W/System.err: at io.reactivex.Scheduler$1.run(Scheduler.java:134)
1542609097.399 26572-26572/com.app.mylife W/System.err: at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
1542609097.399 26572-26572/com.app.mylife W/System.err: at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.lang.Thread.run(Thread.java:764)
1542609097.399 26572-26572/com.app.mylife W/System.err: Caused by: java.net.SocketException: Socket closed
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:203)
1542609097.399 26572-26572/com.app.mylife W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:139)
1542609097.399 26572-26572/com.app.mylife W/System.err: at okio.Okio$2.read(Okio.java:139)
1542609097.399 26572-26572/com.app.mylife W/System.err: at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
1542609097.400 26572-26572/com.app.mylife W/System.err: ... 39 more
Try this code way..
make retrofit object creation part.
class ApiClient {
companion object {
val BASE_URL = "https://simplifiedcoding.net/demos/"
var retrofit: Retrofit? = null
fun getClient(): Retrofit? {
if (retrofit == null) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().apply {
readTimeout(20, TimeUnit.SECONDS)
writeTimeout(20, TimeUnit.SECONDS)
connectTimeout(20, TimeUnit.SECONDS)
addInterceptor(interceptor)
addInterceptor { chain ->
var request = chain.request()
request = request.newBuilder()
.build()
val response = chain.proceed(request)
response
}
}
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit
}
}
}
make interface for api calling..
interface ApiInterface {
#GET("marvel")
fun getData(): Call<List<Hero>>
}
In activity or fragment called api like this way i hope you create response pojo class
var apiInterface: ApiInterface = ApiClient.getClient()!!.create(ApiInterface::class.java)
var hero: Call<List<Hero>>
hero = apiInterface.getData()
hero.enqueue(object : Callback<List<Hero>> {
override fun onFailure(call: Call<List<Hero>>?, t: Throwable?) {
closeDialog(dialog)
Toast.makeText(mContext, t?.message, Toast.LENGTH_SHORT).show()
Log.d("Error:::",t?.message)
}
override fun onResponse(call: Call<List<Hero>>?, response: Response<List<Hero>>?) {
mHeroDataList.clear()
if (response != null && response.isSuccessful && response.body() != null) {
closeDialog(dialog)
mHeroDataList .addAll(response.body()!!)
setAdapter(mHeroDataList)
}
}
})
This code throws Exception when there is no network. So you can catch the exception using try and catch block. You can put try and catch where your are calling the network. And it won't crash your App again.
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"
When my users login success, i do many request to syn my local database.
I do this with retrofit and user RxJava to zip the response.
But when i have a SocketTimeoutException my application crash, and i don't know why.
This is my code :
private fun syncData(): Single<Boolean> {
getProgressTextView().text = getString(R.string.synchronizing)
val list: List<Single<Boolean>> = Arrays.asList(DocumentTypeManager.getAndSave(retrofit),
HomeAccessibilitiesManager.getAndSave(retrofit),
StreetTypesManager.getAndSave(retrofit),
IncidentTypesManager.getAndSave(retrofit),
TaskTypesManager.getAndSave(retrofit, this),
PatientFunituresManager.getAndSave(retrofit, this),
HealthMutualsManager.getAndSave(retrofit))
return Single.zip(list, {
it.forEach {
if (!(it as Boolean)) {
return#zip false
}
}
return#zip true
})
}
an example of an object that get the httpRequest
object HomeAccessibilitiesManager : AnkoLogger {
fun getAndSave(retrofit: Retrofit): Single<Boolean> {
val t = retrofit.create(RestApi::class.java)
.getHomeAccessibilities()
return HttpRequestDataManager.getAndSave(t)
}
}
the manager that save my webService return in database
object HttpRequestDataManager : AnkoLogger {
inline fun <reified T : io.realm.RealmModel> getAndSave(single: Single<List<T>>): Single<Boolean> {
return Single.create { subscriber ->
single
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ list ->
executeTransaction {
list.saveAll()
}
debug { "success sync, with ${list.size} results" }
subscriber.onSuccess(true)
},
{
if (it != null) {
warn { it.message }
subscriber.onError(it)
} else {
subscriber.onError(Throwable("unknow error"))
}
}
)
}
}
}
and the RestApi
#GET("api/v1/home_accessibilities")
fun getHomeAccessibilities(): Single<List<RHomeAccessibilities>>
i also have an JWTInterceptor
class JwtInterceptor(val context: Context) : Interceptor, AnkoLogger {
override fun intercept(chain: Interceptor.Chain): Response {
val builder = chain.request().newBuilder()
if (UserManager.jwtToken.isNotEmpty()) {
builder.header("Authorization", String.format("%s %s", "Bearer", UserManager.jwtToken))
}
val response = chain.proceed(builder.build())
if (response.code() == 401 && UserManager.islogged) {
UserManager.logout()
executeTransaction {
debug { "deleting all database" }
it.deleteAll()
}
val intent = Intent(context, LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent)
context.runOnUiThread { context.toast(context.getString(R.string.session_expired)) }
}
return response
}
}
and the logs :
05-22 09:31:25.771 21960-22425/com.sanilea.itineraire D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.771 21960-21960/com.sanilea.itineraire W/HttpRequestDataManager: SSL handshake timed out
05-22 09:31:25.772 21960-21960/com.sanilea.itineraire W/System.err: io.reactivex.exceptions.UndeliverableException: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.774 21960-21960/com.sanilea.itineraire W/System.err: at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
at io.reactivex.internal.operators.single.SingleCreate$Emitter.onError(SingleCreate.java:82)
at com.sanilea.itineraire.managers.StreetTypesManager$getAndSave$$inlined$getAndSave$1$2.accept(HttpRequestDataManager.kt:52)
at com.sanilea.itineraire.managers.StreetTypesManager$getAndSave$$inlined$getAndSave$1$2.accept(HttpRequestDataManager.kt:21)
at io.reactivex.internal.observers.ConsumerSingleObserver.onError(ConsumerSingleObserver.java:47)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:79)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
05-22 09:31:25.775 21960-21960/com.sanilea.itineraire W/System.err:
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
05-22 09:31:25.776 21960-21960/com.sanilea.itineraire W/System.err:
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
05-22 09:31:25.777 21960-21960/com.sanilea.itineraire W/System.err: Caused by: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.778 21960-21960/com.sanilea.itineraire W/System.err:
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:351)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:302)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:270)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:162)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
05-22 09:31:25.779 21960-21960/com.sanilea.itineraire 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)
05-22 09:31:25.780 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-22 09:31:25.781 21960-21960/com.sanilea.itineraire 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)
05-22 09:31:25.782 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
05-22 09:31:25.783 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
05-22 09:31:25.786 21960-21960/com.sanilea.itineraire W/System.err:
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:212)
05-22 09:31:25.787 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
05-22 09:31:25.790 21960-21960/com.sanilea.itineraire W/System.err:
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
05-22 09:31:25.791 21960-21960/com.sanilea.itineraire W/System.err:
at com.sanilea.itineraire.http.JwtInterceptor.intercept(JwtInterceptor.kt:36)
05-22 09:31:25.792 21960-21960/com.sanilea.itineraire 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)
05-22 09:31:25.793 21960-21960/com.sanilea.itineraire W/System.err:
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:10981)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:10981)
at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
at io.reactivex.Single.subscribe(Single.java:2801)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
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:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
05-22 09:31:25.794 21960-21960/com.sanilea.itineraire W/System.err:
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
05-22 09:31:25.798 21960-22426/com.sanilea.itineraire D/OkHttp: <-- HTTP FAILED: java.net.SocketTimeoutException: SSL handshake timed out
05-22 09:31:25.802 21960-21965/com.sanilea.itineraire I/zygote: Do partial code cache collection, code=122KB, data=78KB
05-22 09:31:25.809 21960-21965/com.sanilea.itineraire I/zygote: After code cache collection, code=122KB, data=78KB
05-22 09:31:25.810 21960-21965/com.sanilea.itineraire I/zygote: Increasing code cache capacity to 512KB
The problem come from my emulator, it was totaly broken...
I increased the timeout of okhttp client to 5 minutes:
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(5, TimeUnit.MINUTES);
client.setReadTimeout(5, TimeUnit.MINUTES);
client.setWriteTimeout(5, TimeUnit.MINUTES);
client.interceptors().add(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Customize the request
Request request = original.newBuilder()
//.header("Accept", "application/json")
.header("Authorization", Constants.SERVICE_AUTH_KEY)
.method(original.method(), original.body())
.build();
// Customize or return the response
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.SERVICE_URL)
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
I use WCF Webservice, i also increased the timeout time to same thing.
Everything works perfectly fine, but uploading file or image to the server.
Even after increasing timeout on both Android and WCF, I still get the error on android:
java.net.SocketException: sendto failed: ETIMEDOUT (Connection timed
out)
This is the full log
W/System.err: java.net.SocketException: sendto failed: ETIMEDOUT (Connection timed out)
W/System.err: at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:550)
W/System.err: at libcore.io.IoBridge.sendto(IoBridge.java:519)
W/System.err: at java.net.PlainSocketImpl.write(PlainSocketImpl.java:511)
W/System.err: at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
W/System.err: at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
W/System.err: at okio.Okio$1.write(Okio.java:80)
W/System.err: at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
W/System.err: at okio.RealBufferedSink.write(RealBufferedSink.java:46)
W/System.err: at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:302)
W/System.err: at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
W/System.err: at okio.RealBufferedSink.writeAll(RealBufferedSink.java:104)
W/System.err: at com.squareup.okhttp.RequestBody$3.writeTo(RequestBody.java:118)
W/System.err: at com.squareup.okhttp.MultipartBuilder$MultipartRequestBody.writeOrCountBytes(MultipartBuilder.java:277)
W/System.err: at com.squareup.okhttp.MultipartBuilder$MultipartRequestBody.writeTo(MultipartBuilder.java:297)
W/System.err: at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:887)
W/System.err: at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:749)
W/System.err: at com.squareup.okhttp.Call.getResponse(Call.java:268)
W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
W/System.err: at com.mbh.mbhportal.network.NewPortalService$1.intercept(NewPortalService.java:59)
W/System.err: at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:221)
W/System.err: at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
W/System.err: at com.squareup.okhttp.Call.execute(Call.java:79)
W/System.err: at retrofit.OkHttpCall.execute(OkHttpCall.java:116)
W/System.err: at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:111)
W/System.err: at retrofit.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:88)
W/System.err: at rx.Observable$2.call(Observable.java:162)
W/System.err: at rx.Observable$2.call(Observable.java:154)
W/System.err: at rx.Observable$2.call(Observable.java:162)
W/System.err: at rx.Observable$2.call(Observable.java:154)
W/System.err: at rx.Observable$2.call(Observable.java:162)
W/System.err: at rx.Observable$2.call(Observable.java:154)
W/System.err: at rx.Observable.unsafeSubscribe(Observable.java:8098)
W/System.err: at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
W/System.err: at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
W/System.err: at java.lang.Thread.run(Thread.java:838)
W/System.err: Caused by: libcore.io.ErrnoException: sendto failed: ETIMEDOUT (Connection timed out)
W/System.err: at libcore.io.Posix.sendtoBytes(Native Method)
W/System.err: at libcore.io.Posix.sendto(Posix.java:151)
W/System.err: at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
W/System.err: at libcore.io.IoBridge.sendto(IoBridge.java:517)
W/System.err: ... 40 more
What timeout is this ETIMEDOUT !?