我使用带警报管理器的服务将数据发送到我的服务器。服务代码:
Intent intent = new Intent(this, onAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, sId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int timeInterval = 14400;
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeInterval * 1000,
timeInterval * 1000, pendingIntent);
在onAlarmReceiver中我尝试发送信息:
HTTPPoster.doPost(url, jsonToSendArray);
....
public static class HTTPPoster {
public static HttpResponse doPost(String url, Object c) throws ClientProtocolException, IOException {
HttpClient httpclient = new DefaultHttpClient();
((DefaultHttpClient) httpclient).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
HttpPost request = new HttpPost(url);
HttpEntity entity;
StringEntity s = new StringEntity(c.toString(), HTTP.UTF_8);
s.setContentType("application/json; charset=utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8"));
entity = s;
request.setEntity(entity);
HttpResponse response;
response = httpclient.execute(request);
return response;
}
}
有时我会在服务器10上同时发出相同的请求。如何解决这个问题呢? 感谢。
答案 0 :(得分:0)