[英]Android Retrofit + Rxjava: How to get response on non200 code?
This is how my request looks like: 这是我的请求的样子:
ApiService apiService = retrofit.create(ApiService.class);
Observable<Response<UserUpdateResponse>> response = apiService.updateUser(Utils.getHeader(), object);
response.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::onSuccessUpdate,
this::onErr,
this::hideDialogLoading);
It's supposed to return 'code':'205' 'msg':'successfully update'
. 它应该返回
'code':'205' 'msg':'successfully update'
。 But when server response any code 201,202 (anything not 200) it will go to error. 但是,当服务器响应任何代码201,202(非200)时,它将出错。
Here is the Error. 这是错误。
java.net.ProtocolException: HTTP 205 had non-zero Content-Length: 121
java.net.ProtocolException:HTTP 205的内容长度非零:121
So how do I prevent it from error, or how do I get error body? 那么如何防止它出错,或者如何得到错误正文? Thank you!.
谢谢!。
HTTP response codes have a predefined definition and some have requirements that they must fullfill to be considered a valid HTTP payload. HTTP响应代码具有预定义的定义,并且某些要求必须将其完全填充才能被视为有效的HTTP有效负载。 You cannot redefine what these codes mean for your application and expect well-implemented clients to accept it.
您无法重新定义这些代码对您的应用程序意味着什么,并期望功能良好的客户接受它。
Looking specifically at HTTP 205 - Reset Content , which has the following requirement: 专门查看HTTP 205-重置内容 ,它具有以下要求:
Since the 205 status code implies that no additional content will be provided, a server MUST NOT generate a payload in a 205 response.
由于205状态代码暗示将不提供任何其他内容,因此服务器不得在205响应中生成有效负载。
Generally applications will just return HTTP 200 for all requests and include application-specific error codes in the payload. 通常,应用程序将针对所有请求返回HTTP 200,并在有效负载中包含特定于应用程序的错误代码。 What you're doing does not make much sense.
你在做什么并没有多大意义。
So technically, I can get response 2xx. 因此,从技术上讲,我可以获得响应2xx。 The problem was that server response body in response code 205 that suppose to be null ( https://tools.ietf.org/html/rfc7231#section-6.3.6 ).
问题是响应代码205中的服务器响应主体假定为空( https://tools.ietf.org/html/rfc7231#section-6.3.6 )。 So after set body null on server, android side works fine.
因此,在服务器上将body设置为null之后,android side可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.