0

嗨,我想知道如何并行运行多个 web 服务调用,实际情况是当我的应用程序第一次启动时,我必须调用 4 个不同的 web 服务并填充数据库,然后用户才能与我的应用程序交互。有人可以建议这样做的最佳方法是什么。还有如何使用 AsyncTask 来运行这 4 个网络服务调用。

截至目前,我正在使用异步任务在我的 homeActivty 中进行一次网络服务调用

new DownloadJSON(HomeActivity.this).execute();  

public class DownloadJSON extends AsyncTask<Void, Void, Void> {

    private static Context context;
        public DownloadJSON(Context context){
            this.context = context;
                }
        @Override
    protected Void doInBackground(Void... params) {

    try {
        startWebServiceData();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
        return null 
}
void startWebServiceData() throws IOException {
     String URL = "xxxxxxxxxxxx";
    String SOAP_NAMESPACE = "xxxxxxxxxxxx";
    String METHOD_NAME = "xxxxxxxxxxx";
    String SOAP_ACTION = "xxxxxxxxxxxx";
    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) {

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
} 

有人能指出我正确的方向吗

4

1 回答 1

0

你可以使用 doInBackground 方法参数吗?

new DownloadJSON(HomeActivity.this).execute(<PropertyValue1>); // first property
new DownloadJSON(HomeActivity.this).execute(<PropertyValue2>); // second property
new DownloadJSON(HomeActivity.this).execute(<PropertyValue3>); // third property
new DownloadJSON(HomeActivity.this).execute(<PropertyValue4>);  // fourth property

public class DownloadJSON extends AsyncTask<PropertyInfo, Void, Void> {

    private static Context context;
        public DownloadJSON(Context context){
            this.context = context;
                }
        @Override
    protected Void doInBackground(PropertyInfo... params) {

    try {
        startWebServiceData(params[0]);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
        return null 
}
void startWebServiceData(PropertyInfo propertyValue) throws IOException {
     String URL = "xxxxxx";
    String SOAP_NAMESPACE = "xxxxx";
    String METHOD_NAME = "xxxxx";
    String SOAP_ACTION = "xxxxx";
    SoapObject soapObject;

    soapObject  = new SoapObject(SOAP_NAMESPACE, METHOD_NAME);

    soapObject.addProperty(propertyValue); 

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

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
} 
于 2013-10-09T10:42:22.540 回答