0

I am using the Retrofit library to make a post request to a server through API, sending username and password and getting in response a session token. I am using this token (in the header as bearer token) to make a POST to the server with latitude and longitude to get the data but the method is ending up repeatedly in onFailure method of enqueue().

@POST("api/auth/login/username/{username}/password/{password}")
Call<ResponseBody> login(@Path("username")String username, @Path("password")String password);

@POST("api/products/get")
Call<ResponseBody> getData(@Header("Authorization") String authorization);Call<ResponseBody> 

In MainActivity:

Retrofit.Builder builder=new Retrofit.Builder()
                           .baseUrl("fake url")
                           .addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit=builder.build();
DataApi companyApi=retrofit.create(DataApi.class);
private String Token="";tokenCall=comapnyApi.login("data","!fakedata");
        tokenCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()){
                    try {
                        Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    Toast.makeText(MainActivity.this, "Wrong Username and password", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Couldn't connect to Fake company Server Api", Toast.LENGTH_SHORT).show();
            }
        });
    Call<ResponseBody> Data=companyApi.getData(Token);
    Data.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()){
                try {
                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                Toast.makeText(MainActivity.this, "Api Token Expired", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Couldn't connect to Fake Company Server", Toast.LENGTH_SHORT).show();
        }
    });

1 Answers1

1

I think it is because you make the second request just after you did the first one. enqueue is async so when you do you first request, I mean tokenCall.enqueue(), its response come later so the second api call or Data.enqueue() is invoked with token == "". So make the second api call in onResponse of the first api call.

private String Token="";
tokenCall=comapnyApi.login("data","!fakedata");
    tokenCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()){
                try {
                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                    Token = response.body().string();
                    Call<ResponseBody> Data=companyApi.getData(Token);
                    Data.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if (response.isSuccessful()){
                                try {
                                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                            else {
                                Toast.makeText(MainActivity.this, "Api Token Expired", Toast.LENGTH_SHORT).show();
                            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Couldn't connect to Fake Company Server", Toast.LENGTH_SHORT).show();
        }
    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else {
                Toast.makeText(MainActivity.this, "Wrong Username and password", Toast.LENGTH_SHORT).show();
            }
        }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, "Couldn't connect to Fake company Server Api", Toast.LENGTH_SHORT).show();
            }
        });
Reza
  • 845
  • 13
  • 18
  • This doesn't work do you know how to fix: E/val: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. – NACHIKETA CHAKRABORTY Jan 03 '21 at 14:58
  • NACHIKETA CHAKRABORTY https://stackoverflow.com/questions/29273387/certpathvalidatorexception-trust-anchor-for-certificate-path-not-found-retro give this link a shot – Reza Jan 03 '21 at 16:33
  • NACHIKETA CHAKRABORTY your welcome. so please rate my answer if it was helpful. – Reza Jan 05 '21 at 07:24