0

私のアプリでは、アプリの起動時にデータベースを作成し、サーバーからデータを取得して挿入していますが、これはうまく機能しています。私は以下のように非同期タスクでサーバー呼び出しを書きました

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

}

上記のコードはうまく機能していますが、実際の問題は、更新ボタンを作成したことです。ボタンをクリックすると、テーブルの値が削除され、サーバーの値が更新された場合は新しい値を再挿入する必要があります。

ボタンをクリックすると、別の asynctask2 を実行しています。ここでは、onpreexecute iam で値を削除し、onbackground メソッドで、以前にデータをダウンロードして挿入するために使用した最初の非同期タスクを使用していますが、2 回目は挿入していません。以下の 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() メソッドで 2 回目の行の下で実行されていないことがわかりました。

 dbHelper.open();   
     System.out.println("table creating second time====================");
     dbHelper.insertActualvalues(ActualvalueObject,ActualvolumeObject,SQLITE_TABLE2);  
4

1 に答える 1