2017-06-03 34 views
0

我有兩個來自soap webservice的方法。我在asyntask中調用它們,它是info.java頁面的超類,並用asyntask的onPost方法獲得結果。 info.java/onCreate的調用代碼如下。如何在同一活動中使用兩種Web服務方法?

 try{ 
     PropertyInfo propertyInfo1 = new PropertyInfo(); 
     properties.clear(); 

     propertyInfo1 = new PropertyInfo(); 
     propertyInfo1.setName("Module_id"); 
     propertyInfo1.setType(String.class); 
     propertyInfo1.setValue(Utils.selectedModule_id); 
     properties.add(propertyInfo1); 

     new Info.AsyncTaskService().execute(new ServiceParams("GetInfo", properties), new ServiceParams("GetInfo_Photo", properties)); 

    } catch (Exception e) { 
     Toast.makeText(Info.this, "Please check your internet connection.", Toast.LENGTH_LONG); 
    } 

這兩種服務方法都採用相同的屬性,這就是爲什麼我給了他們相同的屬性。我的問題是我不能接受結果,因爲我知道它需要在不同的線程中調用這兩個方法,但我不知道如何去做。請問你能幫幫我嗎? asynctask類的代碼也在下面謝謝你。

public class AsyncTaskService extends AsyncTask<ServiceParams, Void, Void> { 
    String resp = ""; 
    String resp2 = ""; 
    ProgressDialog progressDialog; 

    @Override 
    protected Void doInBackground(ServiceParams... params) { 
     resp = WebService.invoke(params[0].properties, params[0].methodName); 
     resp2 = WebService.invoke(params[1].properties, params[1].methodName); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     Log.w("WEBSERVICE RESPONSE===", resp); 

     Log.w("WEBSERVICE RESPONSE===", resp2); 
     try { 
      JSONArray ja = new JSONArray(resp); 
      Utils.subMenuArrayList.clear(); 
      Info_Item info_item=new Info_Item(ja.getJSONObject(0)); 
      ((TextView)findViewById(R.id.txtInfo)).setText(info_item.getInfo()); 
      ((TextView)findViewById(R.id.txtModule)).setText(Utils.selectedMenuName); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     if (progressDialog != null) 
      progressDialog.dismiss(); 
    } 
    @Override 
    protected void onPreExecute() { 

     progressDialog = new ProgressDialog(Info.this); 
     if (progressDialog != null) { 
      progressDialog.setCancelable(false); 
      progressDialog.setMessage("İşlem yapılıyor ..."); 
      progressDialog.show(); 
     } 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     if (progressDialog != null) 
      progressDialog.setProgress(progress[0]); 
    } 

} 

回答

0

我發現如何做到這一點!也想和你分享。

首先描述如下的異步任務。我有兩個方法,我想同時在一個活動中使用(paralel),所以我描述了兩個異步任務類。

public class FirstAsyncTask extends AsyncTask<ServiceParams, Void, Void> { 
    String resp = ""; 
    ProgressDialog progressDialog; 

    @Override 
    protected Void doInBackground(ServiceParams... params) { 
     resp = WebService.invoke(params[0].properties, params[0].methodName); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     Log.w("WEBSERVICE RESPONSE===", resp); 

     try { 
      JSONArray ja = new JSONArray(resp); 
      Utils.subMenuArrayList.clear(); 
      Info_Item info_item=new Info_Item(ja.getJSONObject(0)); 
      ((TextView)findViewById(R.id.txtInfo)).setText(info_item.getInfo()); 
      ((TextView)findViewById(R.id.txtModule)).setText(Utils.selectedMenuName); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     if (progressDialog != null) 
      progressDialog.dismiss(); 
    } 

    @Override 
    protected void onPreExecute() { 

     progressDialog = new ProgressDialog(Info.this); 
     if (progressDialog != null) { 
      progressDialog.setCancelable(false); 
      progressDialog.setMessage("İşlem yapılıyor ..."); 
      progressDialog.show(); 
     } 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     if (progressDialog != null) 
      progressDialog.setProgress(progress[0]); 
    } 

} 

然後你應該在activity的onCreate方法中用executeOnExecuter調用這樣的任務。我在這裏使用了一個屬性數組來存放我要發送給Web服務方法的參數,並描述一個具有屬性和方法名稱的服務參數,並用executeOnExecuter()方法發送它們。我爲我的兩個Web服務方法使用了相同的屬性,但您可以像這樣描述其他屬性數組「private ArrayList properties = new ArrayList <>();」並添加您將要發送到Web服務方法的參數所需的信息。

try{ 
     PropertyInfo propertyInfo1 = new PropertyInfo(); 
     properties.clear(); 

     propertyInfo1 = new PropertyInfo(); 
     propertyInfo1.setName("Module_id"); 
     propertyInfo1.setType(String.class); 
     propertyInfo1.setValue(Utils.selectedModule_id); 
     properties.add(propertyInfo1); 

     ServiceParams serviceparams=new ServiceParams("GetInfo", properties); 
     ServiceParams serviceparams2=new ServiceParams("GetInfo_Photo", properties); 

     FirstAsyncTask asyncTask = new FirstAsyncTask(); // First 
     asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, serviceparams); 
     SecondAsyncTask asyncTask2 = new SecondAsyncTask(); // Second 
     asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, serviceparams2); 

    } catch (Exception e) { 
     Toast.makeText(Info.this, "Please check your internet connection.", Toast.LENGTH_LONG); 
    } 
相關問題