我对asynctask有一些问题......
我使用asynctask将一些URL(* .aspx和paramters)发送到我的服务器并插入一些数据。 我的问题是如何确保数据在服务器中成功插入。然后,如果它可以插入我的服务器,我将在手机上更新数据发送成功的数据库
这是我的网址:
vURL = Routines.URL_SEND_SJ +"?sjnumber=SJ-" + Routines.getSJNumber(vContext) +
"&orgid=" + Routines.getOrgId(vContext) +
"&deviceid=" + Routines.getImei(vContext) +
"&savedate=" + vDate +
"&savetime=" + vTime +
"&status=" + vStatusSJNumber +
"&latitude=" + vLatitude +
"&longitude=" + vLongitude +
"&phonetype=" + vPhoneType +
"&km=" + vKM;
AsyncTask类通过ASPX类发送URL
公共类ASPX {
public static String submitASPXText(String vURL){
String vASPXText="";
HttpClient vHttpClient = new DefaultHttpClient();
HttpGet vHttpGet = new HttpGet(vURL);
try{
ResponseHandler<String> responseHandler=new BasicResponseHandler();
vASPXText = vHttpClient.execute(vHttpGet, responseHandler);
vASPXText = vASPXText.replace("<span id=\"Label1\">~", "");
vASPXText = vASPXText.replace("`</span>", Routines.SEND);
}catch (Exception e){
e.printStackTrace();
}
return vASPXText;
}
private class LocationAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... vURL) {
String vASPXText = ASPX.submitASPXText(vURL[0]);
return vASPXText;
}
@Override
protected void onPostExecute(String vASPXResult) {
super.onPostExecute(vASPXResult);
Toast.makeText(getApplicationContext(), "insert sucess", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
为什么不让服务器返回JSON消息,然后让您的应用查看消息以查看数据是否已保存。例如,如果插入成功返回...
{
"returnCode": 0,
"returnMessage": "Data saved!"
}
如果有错误返回
{
"returnCode": 1,
"returnMessage": "Database Error! Data was not saved!"
}
在Android上,您可以使用内置的JSONObject来读取JSON消息。例如......
JSONObject object = JSONObject(json);
int returnCode = object.getInt("returnCode");
if (returnCode == 0){
//Data was saved
} else {
//Data was saved
}