【问题标题】:How can i use two web service methods in the same activity?如何在同一个活动中使用两种 Web 服务方法?
【发布时间】:2017-11-04 15:58:01
【问题描述】:

我有两种来自soap webservice的方法。我在作为 info.java 页面超类的 asyntask 中调用它们,并在 asyntask 的 onPost 方法中获取结果。 info.java/onCreate的调用代码如下。

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

}

【问题讨论】:

    标签: java android web-services android-asynctask ksoap2


    【解决方案1】:

    我已经找到方法了!也想和你分享。

    首先描述如下异步任务。我有两种方法,我想同时在一个活动中使用(并行),所以我描述了两个异步任务类。

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

    然后,您应该在活动的 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多