在我的应用程序中,我正在创建一个数据库,当应用程序启动并从服务器获取数据并插入它时,它运行良好。我在异步任务中编写了服务器调用,如下所示
@Override
protected Void doInBackground(Void... params) {
try {
startWebServiceForExcelData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
void startWebServiceForData() throws IOException {
String URL = "http://10.XXX.36.XXX:8080/UserWeb/user1";
String SOAP_NAMESPACE = "http://webservice.org/";
String METHOD_NAME = "GetMonthlyData";
String SOAP_ACTION = "http://webservice.org/UserWeb/GetMonthlyDataRequest";
SoapObject soapObject;
soapObject = new SoapObject(SOAP_NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envp = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envp.dotNet = true;
envp.setOutputSoapObject(soapObject);
System.out.println("soapObject===>"+envp.bodyOut);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envp);
SoapPrimitive response = (SoapPrimitive)envp.getResponse();
String result = response.toString();
System.out.println("response data from server====="+result);
parseJson(result);
} catch (Exception e){}}
现在我正在解析数据并插入数据库,如下所示
private static void parseJson(String result) {
try {
JSONObject mainObject = new JSONObject(result);
JSONObject productObject = mainObject.getJSONObject(PRODUCT_CATEGORY);
JSONObject ActualObject = productObject.getJSONObject(ACTUALS);
JSONObject ActualvalueObject = ActualObject.getJSONObject(PRODUCT_VALUE);
JSONObject ActualvolumeObject = ActualObject.getJSONObject(PRODUCT_VOLUME);
dbHelper.open();
System.out.println("table creating second time====================");
dbHelper.insertActualvalues(ActualvalueObject,ActualvolumeObject,SQLITE_TABLE2);
System.out.println("parseJson===================="+SQLITE_TABLE2);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
上面的代码运行良好,现在实际的问题是,我创建了一个刷新按钮,当单击该按钮时,我正在删除表中的值,如果服务器中的值更新,我应该重新插入新值。
当单击按钮 iam 运行另一个 asynctask2 时,在 onpreexecute iam 中删除值和在 onbackground 方法中 iam 使用我之前使用的第一个异步任务来下载数据并插入但它没有第二次插入。asynctask2 的代码如下
public class UpdateDatabaseFile extends AsyncTask<String, Integer, Boolean> {
private static MyDBAdapter dbHelper;
private static Context context;
DownloadJSON task = new DownloadJSON(context);// instance of first async task
public UpdateDatabaseFile(Context context){
this.context = context;
dbHelper = new MyDBAdapter(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dbHelper.open();
dbHelper.deleteActualvalues(); // deleting the values in table
}
@Override
protected Boolean doInBackground(String... arg0) {
task.execute();
return true;
}}
why it is not able to insert the values second time ,but inserting first time. can someone point me right direction.
从调试中我发现它没有在 parseJson() 方法中第二次执行以下行。
dbHelper.open();
System.out.println("table creating second time====================");
dbHelper.insertActualvalues(ActualvalueObject,ActualvolumeObject,SQLITE_TABLE2);