0

Help me out. I am using retrofit for my project and I am totally new on it. I am getting the following error. Why I am getting a false response?

2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: Response
2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: 
Response{protocol=http/1.1, code=402, message=Payment Required, 
url=https://quizziyapa.herokuapp.com/getTopics}
2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: Payment Required
2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: okhttp3.ResponseBody$1@647c751
2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: null
2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: false
2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testing 
D/Ayush: retrofit2.OkHttpCall$NoContentResponseBody@4f0bbb6
[Error][1]

JSON Structure

This is my json structure.

{
    "topics": [
        {
            "_id": 2,
            "topic": "abc",
            "imageUrl": "abc12345"
        },
        {
            "_id": 3,
            "topic": "abc",
            "imageUrl": "abc12345"
        }
    ]
}

Topic.java

My Model class

public class Topic {
    int _id;
    String topic;
    String imageUrl;

    public Topic(int _id, String topic, String imageUrl) {
        this._id = _id;
        this.topic = topic;
        this.imageUrl = imageUrl;
    }

    public int getId() {
        return _id;
    }

    public String getTopic() {
        return topic;
    }

    public String getImageUrl() {
        return imageUrl;
    }
}

TopicResponse.java

JSON response class

public class TopicResponse {
    @SerializedName("topics")
    Topic[] topics;

    public TopicResponse(Topic[] topics) {
        this.topics = topics;
    }

    public Topic[] getTopics() {
        return topics;
    }
}

IUserApi.java (API Interface)

public interface IUserApi {
    @GET("getTopics")
    Call<TopicResponse> getTop();
}

MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://quizziyapa.herokuapp.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IUserApi api =  retrofit.create(IUserApi.class);
        Call<TopicResponse> call= api.getTop();
        call.enqueue(new Callback<TopicResponse>() {
            @Override
            public void onResponse(Call<TopicResponse> call, Response<TopicResponse> response) {
                Log.d("Ayush", "Response");
                Log.d("Ayush", String.valueOf(response));
                Log.d("Ayush", String.valueOf(response.message()));
                Log.d("Ayush", String.valueOf(response.errorBody()));
                Log.d("Ayush", String.valueOf(response.body()));
                Log.d("Ayush", String.valueOf(response.isSuccessful()));
                Log.d("Ayush", String.valueOf(response.raw().body()));
            }

            @Override
            public void onFailure(Call<TopicResponse> call, Throwable t) {
                Log.d("Ayush", "Failed \n"+t.getMessage());
            }
        });

Help me out what should I do to solve this issue?

Mustufa Ansari
  • 596
  • 1
  • 7
  • 13
Ayush Raj
  • 3
  • 5

2 Answers2

0

Well, from the looks of it, the problem is not with the code. The response you are receiving is not a success message rather a 402 errorCode with the message stating "Payment Required".

The reason for this as per the official docs of the API you are using says

either the account has become delinquent as a result of non-payment, or the account’s payment method must be confirmed to continue

Check the image below for reference.

official docs for status codes returned.

So basically its the server returning you an error code. and your response.isSuccessful will ultimately be false unless you get a success message in response i.e one of the statuses shown in the image.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
ljk
  • 1,496
  • 1
  • 11
  • 14
  • I have different APIs too which are working fine and this API too is receiving response when sent via Postman – Ayush Raj Feb 20 '20 at 11:03
  • Well my answer was meant for the question why retrofit gives isSuccessful as false. Personally haven't used heroku or set up servers yet, but this seems to be an issue with how the server was set up. Check some posts on stack for help like [this one here](https://stackoverflow.com/questions/24566635/heroku-cannot-get) as your site https://quizziyapa.herokuapp.com/ returns the same "Cannot GET /" result. Good luck! – ljk Feb 20 '20 at 11:55
0

I think I have found a solution. This API is working perfectly fine over browser and Postman too. But if you notice on PostMan, the status is 402Screenshot Postman Response

As per MDN Web Docs

The HTTP 402 Payment Required is a nonstandard client error status response code that is reserved for future use.

Since, retrofit follows status code 200 as success, the response body in your case is null.

However, if you closely look at the debug console, the value (json) is present, but in the errorBody.

Debug Screenshot

I am not sure why Heroku is sending 402 request, but as per ljk's answer, it seems your account is not fully activated.

You can further readout Retrofit Class Response,

Returns true if code() is in the range [200..300).

Atish Agrawal
  • 2,857
  • 1
  • 23
  • 38