当我使用Retrofit2.0和POST时,body为null,但响应代码是5xx? [英] When i use Retrofit2.0 with POST , body is null , but the reponse code is 5xx?

查看:2058
本文介绍了当我使用Retrofit2.0和POST时,body为null,但响应代码是5xx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Gson gson = new Gson(); 
String json = gson.toJson(account);
System.out.println(json);
RequestBody body = RequestBody.create(MediaType.parse(application / json; charset = utf-8),json);
p2PApi.createDepositAcct(body).enqueue(new Callback< Create>(){
@Override
public void onResponse(Call< Create> call,Response< Create> response){
try {

System.out.println(response.code());
System.out.println(response.errorBody()。string());
} catch(例外e){
e.printStackTrace();
}
}

@Override
public void onFailure(Call< Create> call) ,Throwable t){

}
});

这是我的代码,响应成功,但是response.code()= 518,而
响应消息在response.errorBody()中,reponse.body()为空。



为什么要响应518?



518的平均值是什么?

解决方案

正如您从Retrofit API doc中看到的那样



https://square.github.io/retrofit/ 2.x / retrofit /

  public T body()




成功响应的反序列化响应正文。


成功回复是什么意思?

  public boolean isSuccessful()




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


错误实体方法描述

  PU blic okhttp3.ResponseBody errorBody()




不成功的原始响应正文响应。


不成功的响应意味着代码不在范围(200-300)内,这是isSuccessful()函数中的描述。 / p>

如果你的代码是518,那么消息绝对是错误的()



有关HTTP状态的信息代码,通常你可以在这里参考
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes



5xx状态代码是服务器错误,虽然我不知道错误到底是什么,但通常它应该是服务器端错误。



作为RFC7231,6.响应状态代码声明。


状态-code元素是三位整数代码,给出了理解和满足请求的
结果。



HTTP 状态代码是可扩展的
不需要HTTP客户端理解所有已注册状态代码的含义,尽管这种
理解显然是可取的。但是,客户端必须
了解任何状态代码的类,如第一个
数字所示,并将无法识别的状态代码视为等于
该类的x00状态代码,除了
收件人不得缓存具有无法识别的状态代码的响应。



例如,如果无法识别的状态代码为471,则由
客户端收到,客户可以认为其
请求有问题,并将响应视为已收到400(Bad
请求)状态代码
。响应消息通常包含
表示,用于解释状态。


通常,状态代码是预定义的一些常见的用途,它可以扩展。这意味着可以有一些像518这样的状态代码但是如果你在网上搜索没有确切含义(因为它是自定义的http状态代码)。但您可以将其视为服务器端错误之一(5xx错误)


 Gson gson = new Gson();
    String json = gson.toJson(account);
    System.out.println(json);
    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
    p2PApi.createDepositAcct(body).enqueue(new Callback<Create>() {
        @Override
        public void onResponse(Call<Create> call, Response<Create> response) {
            try {

                System.out.println(response.code());
                System.out.println(response.errorBody().string());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<Create> call, Throwable t) {

        }
    });

This is my code , response is successful ,but response.code()=518 , and the reponse message is in response.errorBody() , reponse.body() is null.

so why reponse 518 ?

what mean of 518 ?

解决方案

As you see from Retrofit API doc

https://square.github.io/retrofit/2.x/retrofit/

public T body()

The deserialized response body of a successful response.

What means the successful response?

public boolean isSuccessful()

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

And the errorbody method description

public okhttp3.ResponseBody errorBody()

The raw response body of an unsuccessful response.

The unsuccessful response means the code is not in range (200-300), which is the description in isSuccessful() function.

If your code is 518, the message would be definitely in errorbody()

For the information of HTTP status code, normally you can refer from here https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

5xx status code is for Server error, although I have no idea what exactly the error is 518, but normally it should be server side error.

As the RFC7231,"6. Response Status Codes" stated.

The status-code element is a three-digit integer code giving the result of the attempt to understand and satisfy the request.

HTTP status codes are extensible. HTTP clients are not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. However, a client MUST understand the class of any status code, as indicated by the first digit, and treat an unrecognized status code as being equivalent to the x00 status code of that class, with the exception that a recipient MUST NOT cache a response with an unrecognized status code.

For example, if an unrecognized status code of 471 is received by a client, the client can assume that there was something wrong with its request and treat the response as if it had received a 400 (Bad Request) status code. The response message will usually contain a representation that explains the status.

Normally, the status code is pre-defined for some common use and it can be extended. That's mean it is possible to have some status code like 518 but there is no exact meaning if you search the web (since it is custom http status code). But you can treat it as one of the server side error (5xx error)

这篇关于当我使用Retrofit2.0和POST时,body为null,但响应代码是5xx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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