当我调用HTTP get, put, post或delete时,我正在获得java.net.SocketTimeoutException
。但是,当我再次单击按钮以进行另一个HTTP调用时,它就成功了。看来AsyncTask/doInBackground
不会像这样失效。但是使用LoopJ HTTP库进行的第一个HTTP调用失败了。一个按钮被挂起来调用下面的方法。On my first click
,我得到了上面的exception
,但是当我再次点击时,它是成功的。下面是日志目录
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)
增加超时时间,可能是网络太慢,无法建立连接,也就是试试这个
client.setTimeout(70000);//or u can set any time in this