轮询Android应用程序偶尔会开始抛出ConnectTimeoutException

时间:2014-01-21 16:11:18

标签: android apache-httpclient-4.x

我写了一个Android应用程序,这不是你的经典Android应用程序。它旨在用于自助服务终端环境,并且需要与自助服务终端的持续连接。为了与自助终端进行通信,我使用了一种轮询机制,每500毫秒轮询一次自助服务终端。在应用程序运行一段时间后,我不时地开始接收ConnectTimeoutExceptions。在通信开始再次成功恢复之前,这似乎持续了大约10秒钟。

我觉得我可能错过了一些关于Android如何管理连接池以及我做错了什么的基本知识。

我正在使用apache DefaultHttpClient,我尝试移动我的代码,以便我实际上为每个请求实例化一个全新的HttpClient,但这似乎没有解决问题。

Problem performing HTTP Post operation.
org.apache.http.conn.ConnectTimeoutException: Connect to .. timed out
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at com.infusion.common.transport.http.HttpRestClient$PostRunnable.run(HttpRestClient.java:113)
at java.lang.Thread.run(Thread.java:856)



HttpResponse response = null;
mClient = buildHttpClient();
        SimpleResponse.StatusCode status = SimpleResponse.StatusCode.Fail;
        String payload = "";
        HttpPost httpPost = new HttpPost(mUrl);

        try {
            // create the post arguments
            httpPost.setEntity(mEntity);
            httpPost.setHeader(HTTP.CONN_DIRECTIVE,HTTP.CONN_CLOSE);

            // execute and process response
            response = mClient.execute(httpPost);
            payload = processResponse(response);

            if (response.getStatusLine().getStatusCode() == 200)
                status = SimpleResponse.StatusCode.Success;
            else
                status =  SimpleResponse.StatusCode.Fail;
        } catch (Exception e) {
            // something went wrong - log and return failure
            if (System.currentTimeMillis() - ERROR_REPORT_FREQ > mLastConnectionFailedLogTime) {
                // something went wrong - log and return failure
                Logger.e(getClass().getSimpleName(), "Problem performing HTTP Post operation.", e);
                mLastConnectionFailedLogTime = System.currentTimeMillis();
            }

            if (response != null) {
                // response must be read to end - HttpClient quirk
                processResponseSafely(response);
            }

            status = SimpleResponse.StatusCode.Fail;
        } finally {

            if (mCallback != null) {
                // callback defined - perform it
                mCallback.completed(new SimpleResponse(status, payload));
            }
        }
    }

private String processResponse(HttpResponse response) throws IOException, NullPointerException {
        HttpEntity entity = response.getEntity();
        String result = "";
        String line;

        // flatten out the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
        while ((line = reader.readLine()) != null) {
            result += line;
        }

        // close the reader - return
        reader.close();
        return result;
    }

private HttpClient buildHttpClient() {
        HttpClient client = new DefaultHttpClient();
        HttpParams params = client.getParams();

        // create an http client with a thread-safe connection manager
        ClientConnectionManager connManager = client.getConnectionManager();
        return new DefaultHttpClient(new ThreadSafeClientConnManager(params, connManager.getSchemeRegistry()), params);
    }

0 个答案:

没有答案