验证http帖子

时间:2013-11-11 22:08:31

标签: android

我正在使用以下代码发布到Google表单。如果HTTP响应不成功,则记录不会标记为从sqlite数据库发送,并在下一次同步操作期间重新发送。问题是我看到频繁的双重帖子,因为即使帖子成功也没有收到回复。

有没有更好的方法来确认帖子而不在表格中创建双重帖子?

感谢您的帮助。

private Boolean performPost(String...data){
  prefs = UserData.getPrefs(mContext);
  spreadsheetKey = prefs.getString(UserData.PREF_SPREADSHEET_KEY,"");
  ArrayList<BasicNameValuePair> results = new ArrayList<BasicNameValuePair>();
  HttpPost post = new HttpPost("https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=" + spreadsheetKey);

  int counter = 0;

  for (String s : data){
    results.add(new BasicNameValuePair("entry." + counter + ".single", s));
    counter ++;
  }

 try {
      post.setEntity(new UrlEncodedFormEntity(results));
    } catch (UnsupportedEncodingException e) {
    // Auto-generated catch block
    Log.e(TAG, "An error has occurred", e);
     }
try {
    //client.execute(post);
    HttpResponse response = client.execute(post);
    StatusLine statusLine = response.getStatusLine();
    returnCode = statusLine.getStatusCode();

    Log.d(TAG, statusLine+ "\n"+ returnCode + " " + String.valueOf(post.getEntity()));
    response.getEntity().consumeContent();

} catch (ClientProtocolException e) {
    // Auto-generated catch block

    Log.e(TAG, "client protocol exception", e);
} catch (IOException e) {
    // Auto-generated catch block
    Log.e(TAG, "io exception", e);
}

if (returnCode == 200){
    return true;
}else{
    resultTxt = SYNC_UNSUCCESSFUL_HTTP_ERROR;
    Log.e(TAG,"post unsuccessful, http code :" + String.valueOf(returnCode));

    sendBroadcast();

    return false;
    }
}

0 个答案:

没有答案