Response.isSuccessfull() 为假 [英] Response.isSuccessfull() is false

查看:63
本文介绍了Response.isSuccessfull() 为假的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮帮我.我正在为我的项目使用改造,我对它完全陌生.我收到以下错误.为什么我收到错误回复?

2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testingD/Ayush:回应2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testingD/阿尤什:响应{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_testingD/Ayush:需要付款2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testingD/Ayush: okhttp3.ResponseBody$1@647c7512020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testingD/Ayush:空2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testingD/Ayush:假2020-02-20 15:35:26.230 22602-22602/q.goaldiggers.auth_api_testingD/Ayush:retrofit2.OkHttpCall$NoContentResponseBody@4f0bbb6[错误][1]

JSON 结构

这是我的json结构.

<代码>{话题": [{"_id": 2,"主题": "abc","imageUrl": "abc12345"},{"_id": 3,"主题": "abc","imageUrl": "abc12345"}]}

Topic.java

我的模型类

公共类主题{int_id;字符串主题;字符串 imageUrl;公共主题(int _id,字符串主题,字符串 imageUrl){this._id = _id;this.topic = 主题;this.imageUrl = imageUrl;}公共 int getId() {返回_id;}公共字符串 getTopic() {返回主题;}公共字符串 getImageUrl() {返回图片网址;}}

TopicResponse.java

JSON 响应类

public class TopicResponse {@SerializedName("主题")主题[] 主题;公共主题响应(主题 [] 主题){this.topics = 主题;}公共主题[] getTopics() {返回主题;}}

IUserApi.java(API 接口)

公共接口 IUserApi {@GET("getTopics")呼叫<主题响应>getTop();}

MainActivity.java

Retrofit 改造 = new Retrofit.Builder().baseUrl("https://quizziyapa.herokuapp.com/").addConverterFactory(GsonConverterFactory.create()).建造();IUserApi api = retrofit.create(IUserApi.class);呼叫<主题响应>调用 = api.getTop();call.enqueue(new Callback() {@覆盖public void onResponse(Call call, Response response) {Log.d("Ayush", "响应");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()));}@覆盖public void onFailure(Call<TopicResponse> call, Throwable t) {Log.d("Ayush", "失败\n"+t.getMessage());}});

帮我看看我该怎么做才能解决这个问题?

解决方案

我想我已经找到了解决方案.这个 API 在 browserPostman 上也能正常工作.但是如果你注意到 PostMan,状态是 402

根据

我不确定 Heroku 为何发送 402 请求,但根据 ljk 的回答,您的帐户似乎未完全激活.

你可以进一步读出Retrofit Class Response,

<块引用>

如果 code() 在 [200..300] 范围内,则返回 true.

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

这篇关于Response.isSuccessfull() 为假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆