2013-01-03 52 views
0

我有这段代码通过android应用程序发布数据到服务器。 因此,假设在将数据发布到服务器之前,连接丢失(超出wifi或3G) 或丢失连接期间。 如何确保数据已发布到服务器? 我将服务器端的响应作为反馈消息或作为响应来实现,但您认为这足够吗?如何确保数据已发布(发送)到远程服务器

另一种情况(像银行关键的系统)

假设我发送数据,并将其张贴好(如插入到数据库中),但得到的响应期间可能发生的错误! 从逻辑上说,因为没有收到响应,我会通知用户例如该过程没有发布并且必须重新发布!这是个问题 !!

那么实施或获得此级别保险的最佳方法是什么?

try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(http://www.exampleof.the/page_that_wil_receive_the_data.php?data=example); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     //"ISO-8859-1,utf-8;q=0.7,*;q=0.7" 
     //"iso-8859-1" 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "utf-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 

回答

1

我相信对于这两个问题,HTTP 200响应代码可确保您的请求已成功发布到服务器中。

+0

谢谢你,在这个主题更多http://stackoverflow.com/questions/6118768/posting-a-json-to-a-restful-webservice-getting-the-http-200-ok-but-it-returns –

+0

请勿在您的帖子中使用签名/标语。您的用户箱计为您的签名,您可以使用您的个人资料发布您喜欢的任何关于您自己的信息。 [关于签名/标语的常见问题](http://stackoverflow.com/faq#signatures) –

2

向每个帖子添加一个GUID,并返回一个返回GUID以及该事务的散列的响应。跟踪服务器上的GUID。如果服务器的响应无效,请重试。如果收到具有相同GUID的交易,请将其丢弃,但确认已收到。

+0

感谢您的参与,有用。 –

相关问题