Android:使用Asynctask无法正常更新数据库

时间:2013-09-30 12:50:23

标签: android database android-asynctask

在我的应用程序中,我正在创建一个数据库,当应用程序启动时,从服务器获取数据并插入它工作正常。我在异步任务中编写了服务器调用,如下所示

    @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){}}

现在iam解析数据并插入数据库,如下所示

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);  

1 个答案:

答案 0 :(得分:0)

here。从asynctask线程中启动asynctask是不好的,它应该从UI线程启动。我建议将startWebServiceForData()从您的第一个异步复制到第二个并从那里运行。