第一次HTTP调用时出现SocketTimeoutException

时间:2014-03-27 12:51:41

标签: java android http loopj

当我调用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)

1 个答案:

答案 0 :(得分:0)

增加你的超时时间可能是互联网太慢而无法建立连接,即试试这个

 client.setTimeout(70000);//or u can set any time in this