从android服务多次发送
我使用带有报警管理器的服务将数据发送到我的服务器。服务代码:从android服务多次发送,android,service,http-post,alarmmanager,Android,Service,Http Post,Alarmmanager,我使用带有报警管理器的服务将数据发送到我的服务器。服务代码: Intent intent = new Intent(this, onAlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, sId, intent, PendingIntent.FLAG_CANCEL_CURRENT); alarmManager = (AlarmManager) getSystemSer
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个相同的请求。如何解决这个问题?
谢谢
确保线程安全
使用最后一个请求的时间戳,并在请求正确之前检查是否延迟