Handshake failed on some android devices - android

On some device i have no problems with a communication with the server used with the retrofit framework. I also used several Android versions to verify that the code run. But on some devices (Samsung S8) I got each time an error: 'Handshake failed'. Does anybody have an idea where is the problem? Thanks!
Here is my code:
protected static Retrofit getInstanceWithoutToken() {
final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
final Request original = chain.request();
final Request request = original.newBuilder()
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
return new Retrofit.Builder()
.baseUrl(CommonConstantsRest.REST_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}

The problem was the ssl communication. Adding the following CipherSuite solved the problem
protected static Retrofit getInstanceWithoutToken() {
ConnectionSpec spec = new
ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.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)
.build();
final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClient.connectionSpecs(Collections.singletonList(spec));
httpClient
.addInterceptor(logging)
.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
final Request original = chain.request();
final Request request = original.newBuilder()
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
return new Retrofit.Builder()
.baseUrl(CommonConstantsRest.REST_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}

Related

How to add http interceptor in retrofit in a service generator class

I created a separate service generator class as shown is this guide https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor
ApiServiceGenerator.java
public class ApiServiceGenerator {
private static final String BASE_URL = "http://192.168.0.205/hadia/api/";
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
private static Retrofit retrofit = builder.build();
private static OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
public static <S> S createService(
Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
}
i need to add an Authorization Header to each request,
how do i do that using this static createService method?
Here is how to create an Interceptor for adding header to each request
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "auth-value"); // <-- this is the important line
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
How i add this method to add a Bearer token to each request?
I have a Creator class like this
class Creator {
public static Services newServices() {
final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Services.HOST)
.client(client)
.addConverterFactory(GsonConverterFactory.create(GsonUtils.get()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(Services.class);
}
}
You need to use the http client created when building the retrofit instance.
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient) // This is the line
.addConverterFactory(GsonConverterFactory.create());

Android retrofit add header and also a HttpLoggingInterceptor

I'm currently studying retrofit in android:
and this is my current code:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + Globals.BEARER_TOKEN)
.build();
return chain.proceed(newRequest);
}
}).build();
How can i add my HttpLoggingInterceptor to the client and also at the same time add my header to the client?
You can add both interceptors calling the method addInterceptor twice:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + Globals.BEARER_TOKEN)
.build();
return chain.proceed(newRequest);
}
})
.addInterceptor(interceptor).build();
To add an interceptor to Retrofit you include it while building OkHttpClient,
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor())
Then you build Retrofit using this client,
Retrofit.Builder builder = new Retrofit.Builder()
.client(client);
Retrofit retrofit = builder.build();

OkHttp 504 Gatweway time out error in android

Response{protocol=http/1.1, code=504, message=GATEWAY_TIMEOUT, url=https://************************}
I am getting
code=504, message=GATEWAY_TIMEOUT
in android but the same url got success in iOS
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30*1000, TimeUnit.MILLISECONDS)
.readTimeout(30*1000, TimeUnit.MILLISECONDS)
.writeTimeout(30*1000, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(false)
.build();
Request request = new Request.Builder().url(urlStr).post(formBody)
.addHeader("Authorization", g.getTokenType() + " " + g.getAccessToken())
.addHeader("Content-type", "application/x-www-form-urlencoded")
try {
Response mResponse = client.newCall(request).execute();
String jsonString = mResponse.body().string();
Try This
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(30*1000, TimeUnit.SECONDS)
.readTimeout(30*1000, TimeUnit.SECONDS)
.writeTimeout(30*1000, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
Put the timeout in minutes as per your server speed time requirement.
Try this :-
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.readTimeout(2, TimeUnit.MINUTES);
httpClient.connectTimeout(2, TimeUnit.MINUTES);
httpClient.writeTimeout(2, TimeUnit.MINUTES);
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder builder = original.newBuilder();
builder.method(original.method(), original.body());
builder.header("Accept", "application/json");
if (TOKEN.length() > 0)
builder.header("Authorization", TOKEN);
return chain.proceed(builder.build());
}
});
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
OkHttpClient client = httpClient.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();

android retrofit add custom header

i using retrofit2 and want add some header using OkHttp3 addInterceptor
but not working
this is my code
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.removeHeader("Authorization")
.removeHeader("Content-type")
.removeHeader("User-Agent")
.addHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Accept-Language", "en-US")
.addHeader("User-Agent", ApiConfig.userAgent)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
if (apiInterface == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiConfig.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
apiInterface = retrofit.create(ApiInterface.class);
}
return apiInterface;
please help me.
kind regards
Try this if it helps
RestAdapter.Builder builder = new RestAdapter.Builder()
.setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json;versions=1");
if (isUserLoggedIn()) {
request.addHeader("Authorization", getToken());
}
}
});

Retrofit 2 addQueryParam replacement

In previous retrofit versions it was possible to add an interceptor and use that to add query parameters that were globally needed for example:
.setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade requestFacade) {
requestFacade.addQueryParam("platform", "android");
requestFacade.addQueryParam("app_version", com.package.BuildConfig.VERSION_NAME);
}
})
With the new implementation, it's required that you use OkHttpClient.interceptors. With this new approach, how would one append parameters without removing the original paramaters?
Here is an okhttp implementation --
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
#Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.httpUrl().newBuilder()
.addQueryParameter("platform", "android")
.addQueryParameter("app_version", com.package.BuildConfig.VERSION_NAME)
.build();
Request newRequest = chain.request().newBuilder().url(url).build();
return chain.proceed(newRequest);
}
});
Add the client to your retrofit --
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.example.com")
.client(client)
.build();
With retrofix 2.3.0 the following code can be used:
final OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
#Override
public Response intercept(final Chain chain) throws IOException
{
final Request request = chain.request();
final HttpUrl url = chain.request().url().newBuilder()
.addQueryParameter("platform", "android")
.addQueryParameter("app_version", com.package.BuildConfig.VERSION_NAME)
.build();
final Request newRequest = chain.request().newBuilder().url(url).build();
return chain.proceed(newRequest);
}
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.example.com")
.client(client)
.build();

Categories

Resources