Retrofit 2 enqueue going to failure but getting 200 in http response (Android REST)

Viewed 472

Main call to login:

public void work(){

        api = createService(context, XYZapi.class);
        Call<HashMap<String, String>> repo = api.doLogin(login);

        repo.enqueue(new Callback<HashMap<String, String>>() {
            @Override
            public void onResponse(Call<HashMap<String, String>> call, Response<HashMap<String, String>> response) {
                Headers headers = response.headers();
                mAuth = headers.get("Authorization");
                // get header value
                String cookie = response.headers().get("Set-Cookie");
                // TODO
                if (response.headers().get("Authorization")!= null) {
                    Log.d(TAG, "onResponse: "+response.body());
                    Log.d(TAG, "onResponse: "+response.headers().get("Authorization"));
                    mAuth = response.headers().get("Authorization");
                    callConfig();
                }
                Log.d(TAG, "onResponse error : "+response.errorBody().toString());

            }

            @Override
            public void onFailure(Call<HashMap<String, String>> call, Throwable t) {
                Log.d(TAG, "onFailure: "+t.getCause());
            }
        });

}

Here the call is directly going to Failure. But I'm getting 200 in http log

This is api :

XYZapi

@POST("/api/login")
    Call<HashMap<String, String>> doLogin(@Body LoginPojo login);

createservice

private static Retrofit.Builder builder = new Retrofit.Builder();

public static <S> S createService(Context context, Class<S> serviceClass) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(@NonNull Chain chain) throws IOException {
            Request original = chain.request();
            String credentials = "username@gmail.com" + ":" + "password";
            final String basic =
                    "Bearer " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

            Request request = original.newBuilder()
                    .addHeader("Authorization", basic)
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    }).addInterceptor(logging).build();

    builder.client(okHttpClient);
    builder.baseUrl("https://app.xyz.com");

    Retrofit adapter = builder.addConverterFactory(GsonConverterFactory.create()).build();
    return adapter.create(serviceClass);
}

What I want is : 1. I want to read the header from response having "Authorization"; 2. I want to read the response data and parse the json to objects. 3. I want to call another service on the basis of response code(status).

I was using retrofit 1.9 till now but I need to update it to retrofit for some reasons.

0 Answers
Related