當我調用HTTP get,put,post或delete時,我得到了java.net.SocketTimeoutException
。但是當我再次單擊該按鈕進行另一次HTTP調用時,則成功。看來AsyncTask/doInBackground
不會像這樣失敗。但是第一次HTTP調用使用LoopJ HTTP庫失敗了。掛鉤按鈕以調用下面的方法。 On my first click
,我得到上面的exception
,但是當我再次點擊時,它是成功的。以下是日誌貓。第一次HTTP調用時出現SocketTimeoutException
private void updateStatus(View view) {
final int currentItemPosition = listView.getPositionForView(view);
RadioButton radioButton = (RadioButton) view;
System.out.println("radioButton.getText(): " + radioButton.getText());
System.out.println("currentItemPosition: " + currentItemPosition);
status.set(currentItemPosition, radioButton.getText().toString());
String url = "http://taskmgr-api.herokuapp.com/tasks/" + id.get(currentItemPosition);
String contentType = "application/json";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("description", description.get(currentItemPosition));
jsonObject.put("status", status.get(currentItemPosition));
jsonObject.put("priority", priority.get(currentItemPosition));
jsonObject.put("duedate", dueDate.get(currentItemPosition));
jsonObject.put("id", id.get(currentItemPosition));
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("json String: " + jsonObject.toString());
StringEntity entity = null;
try {
entity = new StringEntity(jsonObject.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(7000);
client.put(context, url, entity, contentType, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println("response: " + response);
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
error.printStackTrace();
}
});
}
03-27 08:23:06.181: W/System.err(4979): java.net.SocketTimeoutException
03-27 08:23:06.181: W/System.err(4979): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
03-27 08:23:06.181: W/System.err(4979): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-27 08:23:06.191: W/System.err(4979): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-27 08:23:06.191: W/System.err(4979): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
03-27 08:23:06.191: W/System.err(4979): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
03-27 08:23:06.191: W/System.err(4979): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
03-27 08:23:06.201: W/System.err(4979): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
03-27 08:23:06.201: W/System.err(4979): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
03-27 08:23:06.201: W/System.err(4979): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
03-27 08:23:06.201: W/System.err(4979): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
03-27 08:23:06.201: W/System.err(4979): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
03-27 08:23:06.201: W/System.err(4979): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
03-27 08:23:06.211: W/System.err(4979): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
03-27 08:23:06.211: W/System.err(4979): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-27 08:23:06.211: W/System.err(4979): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-27 08:23:06.211: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
03-27 08:23:06.211: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
03-27 08:23:06.221: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
03-27 08:23:06.221: W/System.err(4979): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
03-27 08:23:06.221: W/System.err(4979): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-27 08:23:06.221: W/System.err(4979): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-27 08:23:06.221: W/System.err(4979): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-27 08:23:06.231: W/System.err(4979): at java.lang.Thread.run(Thread.java:841)
03-27 08:23:06.231: W/System.err(4979): java.net.SocketTimeoutException
03-27 08:23:06.231: W/System.err(4979): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
03-27 08:23:06.231: W/System.err(4979): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-27 08:23:06.231: W/System.err(4979): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-27 08:23:06.231: W/System.err(4979): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
03-27 08:23:06.231: W/System.err(4979): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-27 08:23:06.241: W/System.err(4979): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-27 08:23:06.241: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
03-27 08:23:06.241: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
03-27 08:23:06.241: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
03-27 08:23:06.241: W/System.err(4979): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
03-27 08:23:06.241: W/System.err(4979): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-27 08:23:06.241: W/System.err(4979): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-27 08:23:06.241: W/System.err(4979): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-27 08:23:06.241: W/System.err(4979): at java.lang.Thread.run(Thread.java:841)
03-27 08:23:06.251: W/System.err(4979): java.net.SocketTimeoutException
03-27 08:23:06.251: W/System.err(4979): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
03-27 08:23:06.251: W/System.err(4979): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
03-27 08:23:06.251: W/System.err(4979): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-27 08:23:06.251: W/System.err(4979): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-27 08:23:06.251: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
03-27 08:23:06.251: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
03-27 08:23:06.251: W/System.err(4979): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
03-27 08:23:06.251: W/System.err(4979): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
03-27 08:23:06.251: W/System.err(4979): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-27 08:23:06.261: W/System.err(4979): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-27 08:23:06.261: W/System.err(4979): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-27 08:23:06.261: W/System.err(4979): at java.lang.Thread.run(Thread.java:841)
而且,似乎這個錯誤並不一致。我已經刪除了應用程序,有時它可以工作,有時它不會。 –