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?
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 402
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
.
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).