gpt4 book ai didi

android - java.net.ProtocolException : Unexpected status line: HTTP/1. 1 422不可处理的实体

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:54 25 4
gpt4 key购买 nike

我正在使用 Retrofit + Okhttp 发出 POST 请求,但我遇到了以下错误:

02-05 04:45:13.981  15972-16249/com.myapp.android D/Retrofit﹕ ---> HTTP POST http://10.0.0.4:3000/api/v1/users/1/posts
02-05 04:45:13.981 15972-16249/com.myapp.android D/Retrofit﹕ Accept: application/json
02-05 04:45:13.981 15972-16249/com.myapp.android D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
02-05 04:45:13.981 15972-16249/com.myapp.android D/Retrofit﹕ Content-Length: 150
02-05 04:45:13.981 15972-16249/com.myapp.android D/Retrofit﹕ {"description":"test","image_url":"https://s3.amazonaws.com/bucket/xxx-4800-b0e0-fc206f95f158.jpeg","title":"test","price":0.0,"user_id":0}
02-05 04:45:13.981 15972-16249/com.myapp.android D/Retrofit﹕ ---> END HTTP (150-byte body)
02-05 04:45:14.001 15972-15972/com.myapp.android W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
02-05 04:45:14.017 15972-16249/com.myapp.android D/Retrofit﹕ ---- ERROR http://10.0.0.4:3000/api/v1/users/1/posts
02-05 04:45:14.017 15972-16249/com.myapp.android D/Retrofit﹕ java.net.ProtocolException: Unexpected status line: HTTP/1.1 422��Unprocessable Entity
at com.squareup.okhttp.internal.http.StatusLine.parse(StatusLine.java:73)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:791)
at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:90)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:784)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:645)
at com.squareup.okhttp.Call.getResponse(Call.java:263)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:219)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:192)
at com.squareup.okhttp.Call.execute(Call.java:79)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
at retrofit.RxSupport$2.run(RxSupport.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:841)
02-05 04:45:14.017 15972-16249/com.myapp.android D/Retrofit﹕ ---- END ERROR

使用以下库:

compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'

我希望返回类似以下内容(有意测试 422 错误):

{
"title": [
"is too short (minimum is 6 characters)"
]
}

通过 Postman ( https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en ) 发送时我得到了预期的结果

编辑:我在 Rails 中进行以下调用以返回此 422 结果:

render json: @post.errors.to_json, status: :unprocessable_entity

另一个编辑:

这是通过 Wireshark 发出的请求的屏幕截图。看起来没什么异常,但我以前也从未使用过该工具。

Overall

这是适当的十六进制。

Hex

最佳答案

您的服务器似乎违反了 HTTP 规范。状态行定义如下:

The first line of a response message is the status-line, consisting of the protocol version, a space (SP), the status code, another space, a possibly empty textual phrase describing the status code, and ending with CRLF.

status-line = HTTP-version SP status-code SP reason-phrase CRLF

从异常看来,状态代码和原因短语之间有一个(或两个)非空格字符。

OkHttp 正在寻找这些元素之间的 ASCII 空格(字符 #32)。您可以使用 WireShark 或 Charles 之类的工具来拦截网络流量并找出您的 HTTP 服务器正在使用哪些字符。

关于android - java.net.ProtocolException : Unexpected status line: HTTP/1. 1 422不可处理的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28336457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com