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();
}
}
感謝您的幫助
如何,並從那裏,是被稱爲你的`run`方法? – 2011-02-11 16:06:42