简体   繁体   English

当我将Retrofit2.0与POST一起使用时,body为null,但响应代码为5xx?

[英]When i use Retrofit2.0 with POST , body is null , but the reponse code is 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. 这是我的代码,响应成功,但是response.code()= 518,并且响应消息在response.errorBody()中,reponse.body()为null。

so why reponse 518 ? 那为什么要回应518?

what mean of 518 ? 518是什么意思?

As you see from Retrofit API doc 从Retrofit API文档中可以看到

https://square.github.io/retrofit/2.x/retrofit/ 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). 如果code()在[200..300)范围内,则返回true。

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. 响应失败表示代码不在范围(200-300)中,这是isSuccessful()函数中的描述。

If your code is 518, the message would be definitely in errorbody() 如果您的代码是518,则该消息肯定在errorbody()中

For the information of HTTP status code, normally you can refer from here https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 有关HTTP状态代码的信息,通常您可以从这里参考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. 5xx状态代码用于服务器错误,虽然我不知道该错误确切是518,但是通常它应该是服务器端错误。

As the RFC7231,"6. Response Status Codes" stated. 如RFC7231所述,“ 6。响应状态代码”。

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 状态代码是可扩展的 HTTP clients are not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. HTTP客户端不需要了解所有已注册状态代码的含义,尽管显然希望这样。 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. 但是,客户端必须理解任何状态代码的类别(如第一位数字所示),并将无法识别的状态代码视为等同于该类别的x00状态代码,不同之处在于接收者不得使用以下方式缓存响应:无法识别的状态码。

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 . 例如,如果客户端接收到无法识别的状态代码471,则客户端可以假定其请求有问题,并将响应视为已接收到400(错误请求)状态代码 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). 这意味着可能有一些类似518的状态代码,但是如果您在网上搜索,则没有确切的含义(因为它是自定义的http状态代码)。 But you can treat it as one of the server side error (5xx error) 但是您可以将其视为服务器端错误之一(5xx错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM