0

In my app i am creating a database when the app starts and from server getting data and inserting it which is working good. i wrote the server call in a async task as below

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

Now iam parsing the data and inserting in database as below

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

}

The above code is working good, now the actual problem is , i created a refresh button , when clicking the button i am deleting the values in the table and i should reinsert it new values if the values in server updated.

when clicking the button iam running another asynctask2 , here in the onpreexecute iam deleting the values and in onbackground method iam using the first async task which i used before to download data and insert but it is not inserting second time. code for asynctask2 as below

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.

From debugging i found that its not executing below lines second time in parseJson() method.

 dbHelper.open();   
     System.out.println("table creating second time====================");
     dbHelper.insertActualvalues(ActualvalueObject,ActualvolumeObject,SQLITE_TABLE2);  
teekib
  • 2,821
  • 10
  • 39
  • 75

1 Answers1

0

See here. It's bad to start an asynctask from within an asynctask thread, it should be started from the UI thread. I suggest copying startWebServiceForData() from your first async to the second and running it from there.

Community
  • 1
  • 1
dlee
  • 78
  • 9
  • 1
    Exactly u are right the second async task should run on the ui thread: thanks for the link, its exactly what i want, now my app is working as expected . thanks a lot. – teekib Sep 30 '13 at 13:27