2014-10-01 68 views
0

我使用帶有鬧鐘管理器的服務將數據發送到我的服務器。從服務代碼:來自android服務的多重發送

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
  1. 使其線程最後請求,並檢查安全
  2. 使用時間戳請求之前延遲是正確的