开发者

Timer and DefaultHttpClient

I want to receive json string every minute from https server. Emulator works nice, but on device my widget stops to update info (ignores new json strings) after about 30 minutes. First 30 minutes widget works perfectly.

Timer:

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();
    }
}

Thanks for help


I had a similar problems. The issue in my case was in requests concurrency and limit in resources.

My solution:

Redo configuration of DefaultHttpClient to:

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);

And call entity.consumeContent(); after your // Closing the input stream will trigger connection release instream.close(); to release resources

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜