Timer和DefaultHttpClient

时间:2011-02-11 15:02:29

标签: android

我想每分钟从https服务器接收json字符串。模拟器工作得很好,但在设备上我的小部件在大约30分钟后停止更新信息(忽略新的json字符串)。前30分钟小部件完美运行。

计时器:

public void run() {
        client = new RestClient("https://example.com/check_messages_new.php");      
        if (userName != null)
        {
            client.AddParam("user", userName);
            client.AddParam("output", "json");

            try {
                client.Execute(RequestMethod.GET);
            } catch (Exception e) {
                connect = false;
                e.printStackTrace();
            }
        }

RestClient.Execute():

    response = new String[3];
    response[0] = "0";
    Log.d(LOG_TAG, "response[0] set to 0");

    HttpParams httpParameters = new BasicHttpParams();

    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = 5000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);

    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 25000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);


    HttpClient client = new DefaultHttpClient(httpParameters);
    //HttpConnectionParams.setConnectionTimeout(client.getParams(), 25000);         

    HttpResponse httpResponse;

    try {
        httpResponse = client.execute(request);

        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStream(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e)  {
        response[0] = "0";
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        response[0] = "0";
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    }
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。我的问题是请求并发和资源限制。

我的解决方案:

将DefaultHttpClient重做配置为:

final AbstractHttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 5000);
    ConnManagerParams.setMaxTotalConnections(httpParameters, 20);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

    final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParameters, schemeRegistry); 

    client = new DefaultHttpClient(cm, httpParameters); 
    final HttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(3, true);
    client.setHttpRequestRetryHandler(retryHandler);

entity.consumeContent();发布资源后调用// Closing the input stream will trigger connection release instream.close();