这真的很奇怪。我有一个看起来几乎相同但不会引发异常的函数。这是有问题的代码:
public Header[] generateHeaders() {
Header[] headers = new Header[2];
headers[0] = new BasicHeader("Client", "android");
headers[1] = new BasicHeader("Accept", "application/json; charset=UTF-8");
return headers;
}
public HttpPost getHttpRequestPost() throws URISyntaxException {
HttpPost request = new HttpPost(new URI(generateQueryUrl()));
request.setHeaders(generateHeaders());
setTimeout(request);
return request;
}
protected Query buildQuery() {
Query query = null;
query = new Query(new Config(), getContext());
query.addOrderedParam(FUNCTION_NAME);
return query;
}
下面的代码导致ClientProtocolException
,正是这个:Caused by: org.apache.http.ProtocolException: Invalid header: :
public HttpResponse execute() throws URISyntaxException, ClientProtocolException,
IOException {
HttpClient client = new DefaultHttpClient();
HttpResponse response = response = client.execute(buildQuery().getHttpRequestPost());
return response;
}
它不指向 2 个标题中的任何一个,而是指向一些空标题。这里发生了什么?
client.execute(...) 内部发生异常,显然我无法在那里调试...
这些可能是响应标头...但是我在提琴手中看不到任何问题。这是请求和响应正文:
要求
GET http://myapp.api.pl/api/bankName?accountNumber=32195000012006548541990002 HTTP/1.1
User-Agent: Fiddler
Accept: application/json
Client: android
Host: myapp.api.pl
回复
HTTP/1.1 404 Not Found
Date: Tue, 12 Nov 2013 08:35:01 GMT
Server: Apache
X-Powered-By: PleskLin
Content-Length: 0
Connection: close
Content-Type: application/json; charset=UTF-8
好吧,我撒了一点谎,在提琴手中并非一切都好。如果参数 accountNumber 指定了错误的数字,函数应该返回 404。确实如此,但在提琴手中也有错误弹出:
编辑:粘贴请求的堆栈跟踪:
11-08 13:09:30.691: W/System.err(20729): org.apache.http.client.ClientProtocolException
11-08 13:09:30.701: W/System.err(20729): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
11-08 13:09:30.711: W/System.err(20729): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-08 13:09:30.711: W/System.err(20729): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-08 13:09:30.721: W/System.err(20729): at pl.test.helloapp.communication.requests.BaseRequest.execute(BaseRequest.java:75)
11-08 13:09:30.721: W/System.err(20729): at pl.test.helloapp.async.LoginAsyncTask.doInBackground(LoginAsyncTask.java:51)
11-08 13:09:30.721: W/System.err(20729): at pl.test.helloapp.async.LoginAsyncTask.doInBackground(LoginAsyncTask.java:1)
11-08 13:09:30.721: W/System.err(20729): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-08 13:09:30.731: W/System.err(20729): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-08 13:09:30.731: W/System.err(20729): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-08 13:09:30.731: W/System.err(20729): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-08 13:09:30.731: W/System.err(20729): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-08 13:09:30.731: W/System.err(20729): at java.lang.Thread.run(Thread.java:841)
11-08 13:09:30.731: W/System.err(20729): Caused by: org.apache.http.ProtocolException: Invalid header: :
11-08 13:09:30.731: W/System.err(20729): at org.apache.http.impl.io.AbstractMessageParser.parseHeaders(AbstractMessageParser.java:162)
11-08 13:09:30.731: W/System.err(20729): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:178)
11-08 13:09:30.741: W/System.err(20729): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
11-08 13:09:30.741: W/System.err(20729): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
11-08 13:09:30.741: W/System.err(20729): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
11-08 13:09:30.741: W/System.err(20729): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
11-08 13:09:30.741: W/System.err(20729): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
11-08 13:09:30.741: W/System.err(20729): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
11-08 13:09:30.751: W/System.err(20729): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-08 13:09:30.751: W/System.err(20729): ... 11 more