在我的应用程序中,我通过 json 格式的 post 方法将数据发送到服务器。在这里你可以看到我的代码。
JSONObject object1 = new JSONObject();
object1.put("homesafe_events_version", "1.0");
JSONObject object = new JSONObject();
object.put("PhoneNumber", phoneNumber);
object.put("EventTypeID", Integer.parseInt(eventTypeId));
object.put("FreeFromText", "");
object.put("EventTime", eventTime );
object.put("Latitude", latitude);
object.put("Longitude", longitude);
object.put("Altitude", Double.parseDouble(df.format(altitude)));
object.put("HorizAccuracy",Double.parseDouble(df.format(horizAccuracy)));
object.put("VertAccuracy", vertiAccuracy);
object.put("Speed", speed);
object.put("Heading", Double.parseDouble(df.format(heading)));
object.put("BatteryStatus", batteryStatus);
object1.put("Event", object);
String str = object1.toString();
HttpClient client = new DefaultHttpClient();
String encodedURL = URLEncoder.encode(str, "UTF-8");
HttpPost request = new HttpPost(url);
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("Name", encodedURL));
// UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);
StringEntity entity = new StringEntity(str, "UTF8");
request.setHeader("Content-Type", "application/json");
request.setEntity(entity);
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);
一切正常。我的应用程序中有一个按钮。单击该按钮时,会计算一些值并传入参数以将它们发送到服务器。现在假设互联网不可用,然后我按下该按钮 5 次在这种情况下,计算 5 次的所有值都存储在其他地方,当连接到来时,Web 服务将命中,所有数据都将发送到服务器。这是我的实际问题。请建议我该怎么做。提前致谢 !!