Android:使用asynctask并行运行的多个Web服务调用

时间:2013-10-09 10:25:39

标签: android multithreading android-asynctask

您好我想知道如何并行运行多个Web服务调用,实际情况是我的应用程序第一次启动时,我必须先调用4个不同的Web服务并填写数据库,然后用户才能与我的应用程序进行交互。有人可以建议这样做的最佳方法。 另外,如何使用AsyncTask来运行这4个webservice调用。

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

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

}
} 

可以指出我正确的方向吗

1 个答案:

答案 0 :(得分: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();
    }

}
}