2011-02-11 58 views
0

我想從https服務器每分鐘接收json字符串。模擬器工作的很好,但在設備上,我的小部件在大約30分鐘後停止更新信息(忽略新的json字符串)。第一個30分鐘的小部件完美工作定時器和DefaultHttpClient

定時器:

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

感謝您的幫助

+0

如何,並從那裏,是被稱爲你的`run`方法? – 2011-02-11 16:06:42

回答

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();後釋放資源