0

I would like to send https get request to my server using android retrofit2.

If I send same request with http it's working fine.

Whenever I send https request not working.

My Endpoint:

https://xxxx:xxx@xxx/rest/xxx

My code snippet:

String base = uname + ":" + pwd;

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        Request.Builder requestBuilder = original.newBuilder()
            .header("Authorization", base);

        Request request = requestBuilder.build();
        return chain.proceed(request);
    }
});

OkHttpClient client = httpClient.build();

Retrofit rulesRetrofit = new retrofit2.Retrofit.Builder()
    .baseUrl(HttpsRuels)
    .addConverterFactory(GsonConverterFactory.create())
    .client(client)
    .build();
cloudRulesDataInterface = rulesRetrofit.create(APInterface.class);

Call < ResponseBody > rulesCall = cloudRulesDataInterface.getRulesFromCloud();
rulesCall.enqueue(new Callback < ResponseBody > () {
    @Override
    public void onResponse(Call < ResponseBody > call, Response < ResponseBody > response) {
        if (response.isSuccessful()) {
            try {
                Log.d(TAG, "response from success =" + response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                Log.d(TAG, "response from error =" + response.errorBody().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onFailure(Call < ResponseBody > call, Throwable throwable) {

    }
});

Always i am getting Unauthorized response from server. When i was test in postman it's working fine.But in android not working.

some user
  • 1,693
  • 2
  • 14
  • 31
amar544
  • 43
  • 9

1 Answers1

0

Refer to this answer. You need to enable the SSL certificate usage in your retrofit code logic.

HTTPS IN RETROFIT

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
  • Thanks for suggestions reply. This solution worked for me https://futurestud.io/tutorials/android-basic-authentication-with-retrofit – amar544 Mar 06 '20 at 09:42