Response.isSuccessfull() 为假
Response.isSuccessfull() is false
帮帮我。
我正在为我的项目使用改造,我是全新的。我收到以下错误。为什么我收到错误回复?
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@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结构
这是我的 json 结构。
{
"topics": [
{
"_id": 2,
"topic": "abc",
"imageUrl": "abc12345"
},
{
"_id": 3,
"topic": "abc",
"imageUrl": "abc12345"
}
]
}
Topic.java
我的模型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 回复 class
public class TopicResponse {
@SerializedName("topics")
Topic[] topics;
public TopicResponse(Topic[] topics) {
this.topics = topics;
}
public Topic[] getTopics() {
return topics;
}
}
IUserApi.java(API 界面)
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());
}
});
帮我看看我该怎么做才能解决这个问题?
嗯,看来问题不在代码上。您收到的响应不是一条成功消息,而是一条 402 errorCode 消息,其中包含“需要付款”的消息。
根据您正在使用的 API 的 official docs,原因是
either the account has become delinquent as a result of non-payment, or the account’s payment method must be confirmed to continue
查看下图以供参考。
.
所以基本上是服务器向您返回错误代码。并且您的 response.isSuccessful 最终将是错误的,除非您收到成功消息作为响应,即图像中显示的状态之一。
我想我找到了解决办法。这个 API 在 browser
和 Postman
上也工作得很好。但是如果你注意到PostMan
,状态是402
The HTTP 402 Payment Required is a nonstandard client error status response code that is reserved for future use.
由于 retrofit
遵循状态代码 200
表示成功,因此您的响应正文为空。
但是,如果您仔细查看调试控制台,就会发现值 (json
) 存在,但在 errorBody
.
我不确定为什么 Heroku 发送 402
请求,但根据 ljk 的回答,您的帐户似乎没有完全激活。
您可以进一步读出Retrofit Class Response、
Returns true if code() is in the range [200..300).
帮帮我。 我正在为我的项目使用改造,我是全新的。我收到以下错误。为什么我收到错误回复?
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@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结构
这是我的 json 结构。
{
"topics": [
{
"_id": 2,
"topic": "abc",
"imageUrl": "abc12345"
},
{
"_id": 3,
"topic": "abc",
"imageUrl": "abc12345"
}
]
}
Topic.java
我的模型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 回复 class
public class TopicResponse {
@SerializedName("topics")
Topic[] topics;
public TopicResponse(Topic[] topics) {
this.topics = topics;
}
public Topic[] getTopics() {
return topics;
}
}
IUserApi.java(API 界面)
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());
}
});
帮我看看我该怎么做才能解决这个问题?
嗯,看来问题不在代码上。您收到的响应不是一条成功消息,而是一条 402 errorCode 消息,其中包含“需要付款”的消息。
根据您正在使用的 API 的 official docs,原因是
either the account has become delinquent as a result of non-payment, or the account’s payment method must be confirmed to continue
查看下图以供参考。
所以基本上是服务器向您返回错误代码。并且您的 response.isSuccessful 最终将是错误的,除非您收到成功消息作为响应,即图像中显示的状态之一。
我想我找到了解决办法。这个 API 在 browser
和 Postman
上也工作得很好。但是如果你注意到PostMan
,状态是402
The HTTP 402 Payment Required is a nonstandard client error status response code that is reserved for future use.
由于 retrofit
遵循状态代码 200
表示成功,因此您的响应正文为空。
但是,如果您仔细查看调试控制台,就会发现值 (json
) 存在,但在 errorBody
.
我不确定为什么 Heroku 发送 402
请求,但根据 ljk 的回答,您的帐户似乎没有完全激活。
您可以进一步读出Retrofit Class Response、
Returns true if code() is in the range [200..300).