简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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