sarbesh sarkar
sarbesh sarkar

Reputation: 87

Retrofit2 enqueue onResponse and onFailure not executing

am new to android retrofit2. Am trying to load my login response with access and refresh token but .enqueue() is able to connect to backend and get response. But post the response onResponse and onFailure are not getting executed.

code @github

RetrofitInstance.java:

public class RetrofitInstance {
private static Retrofit retrofit = null;


public static Retrofit getRetrofitInstance(String url) {

    if (null==retrofit){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .readTimeout(3000, TimeUnit.MILLISECONDS)
                .connectTimeout(4000, TimeUnit.MILLISECONDS)
                .callTimeout(5000,TimeUnit.MILLISECONDS)
                .writeTimeout(3000,TimeUnit.MILLISECONDS)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //for RXjava
                .client(okHttpClient)
                .build();
    }
    return retrofit;
}}

ApiClient.java

public class ApiClient {

private static final String BASE_URL = "http://192.168.0.10:8080/";

public static LoginService getLoginService(){
    return RetrofitInstance.getRetrofitInstance(BASE_URL).create(LoginService.class);
}}

LoginService.java:

public interface LoginService {

@POST("accounts/api/token/")
Call<LoginResponse> getAuthToken(@Body LoginRequest loginRequest);

@POST("accounts/api/token/refresh/")
Observable<LoginResponse> refreshAuthToken(@Body LoginResponse refreshRequest);}

LoginResponse.java

public class LoginResponse {

@SerializedName("access")
@Expose
private String access;
@SerializedName("refresh")
@Expose
private String refresh;
@SerializedName("detail")
@Expose
private String detail;

public void setAccess(String access) {
    this.access = access;
}

public void setRefresh(String refresh) {
    this.refresh = refresh;
}

public void setDetail(String detail) {
    this.detail = detail;
}

public String getDetail() {
    return detail;
}

public String getAccess() {
    return access;
}

public String getRefresh() {
    return refresh;
}

public LoginResponse(String access, String refresh) {
    this.access = access;
    this.refresh = refresh;
}

public LoginResponse(String detail) {
    this.detail = detail;
}

public LoginResponse() {
}}

login.java

Call<LoginResponse> loginResponseCall = service.getAuthToken(new LoginRequest(username,password));
        CountDownLatch countDownLatch = new CountDownLatch(1);
        loginResponseCall.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                Log.d("#Login", "Result: " + response);
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        loggedInUser = new LoggedInUser(response.body());
                    }
                }else {
                    Log.e("#Login", "error: "+ (response.body() != null ? response.body() : null));
                }
                countDownLatch.countDown();
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
            Log.e("#Login", "Exception at subscription");
            countDownLatch.countDown();
            }
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            Log.e("#Login","Exception in countDownLatch.await() "+e.getMessage());
        }

Error response:

{
  "detail": "No active account found with the given credentials"
}

correct Response:

{
"refresh": "JWT refresh token",
"access": "JWT access token"
}

Logcat:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/okhttp.OkHttpClient: --> POST http://192.168.0.10:8080/accounts/api/token/
I/okhttp.OkHttpClient: Content-Type: application/json; charset=UTF-8
I/okhttp.OkHttpClient: Content-Length: 45
I/okhttp.OkHttpClient: {"password":"password","username":"user"}
I/okhttp.OkHttpClient: --> END POST (45-byte body)
I/okhttp.OkHttpClient: <-- 200 OK http://192.168.0.10:8080/accounts/api/token/ (678ms)
I/okhttp.OkHttpClient: Date: Sun, 06 Jun 2021 13:54:55 GMT
I/okhttp.OkHttpClient: Server: WSGIServer/0.2 CPython/3.9.5
I/okhttp.OkHttpClient: Content-Type: application/json
I/okhttp.OkHttpClient: Vary: Accept
I/okhttp.OkHttpClient: Allow: POST, OPTIONS
I/okhttp.OkHttpClient: X-Frame-Options: DENY
I/okhttp.OkHttpClient: Content-Length: 494
I/okhttp.OkHttpClient: X-Content-Type-Options: nosniff
I/okhttp.OkHttpClient: Referrer-Policy: same-origin
I/okhttp.OkHttpClient: {"refresh":"JWT refresh token","access":"JWT access token"}
I/okhttp.OkHttpClient: <-- END HTTP (494-byte body)

post this countdownlatch does not goes to zero and the thread awaits. Please assist on this. I have also tried with rxjava2 but no luck

Upvotes: 0

Views: 675

Answers (1)

Krishan Madushanka
Krishan Madushanka

Reputation: 389

I had also the same problem. Please add this line in application tag in manifest. I hope it will also help you.

android:usesCleartextTraffic="true"

Upvotes: 0

Related Questions