Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:使用asynctask并行运行多个webservice调用_Android_Multithreading_Android Asynctask - Fatal编程技术网

Android:使用asynctask并行运行多个webservice调用

Android:使用asynctask并行运行多个webservice调用,android,multithreading,android-asynctask,Android,Multithreading,Android Asynctask,您好,我想知道如何并行运行多个Web服务调用,实际情况是当我的应用程序第一次启动时,我必须调用4个不同的Web服务并填充数据库,然后用户才能与我的应用程序交互。有人能建议什么是做这件事的最佳方法吗。 还有如何使用AsyncTask来运行这4个webservice调用 到目前为止,我正在使用异步任务在homeActivty中执行一个webservice调用 new DownloadJSON(HomeActivity.this).execute(); public class Download

您好,我想知道如何并行运行多个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();
    }

}
} 
new DownloadJSON(HomeActivity.this).execute();
公共类下载JSON扩展异步任务{
私有静态语境;
公共下载JSON(上下文){
this.context=上下文;
}
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
startWebServiceData();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
返回空值
}
void startWebServiceData()引发IOException{
字符串URL=“XXXXXXXXXX”;
字符串SOAP_NAMESPACE=“xxxxxxxxxxx”;
字符串方法\u NAME=“xxxxxxxxxx”;
字符串SOAP\u ACTION=“xxxxxxxxxxx”;
SoapObject SoapObject;
soapObject=新的soapObject(SOAP\名称空间、方法\名称);
SoapSerializationEnvelope envp=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envp.dotNet=true;
环境setOutputSoapObject(soapObject);
System.out.println(“soapObject==>”+envp.bodyOut);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
试一试{
调用(SOAP_操作,envp);
SoapPrimitive响应=(SoapPrimitive)envp.getResponse();
字符串结果=response.toString();
System.out.println(“来自服务器的响应数据===”+结果);
parseJson(结果);
}捕获(例外e){
}}
私有静态void parseJson(字符串结果){
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
} 

有人能给我指出正确的方向吗你能用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();
    }

}
} 
new DownloadJSON(HomeActivity.this).execute();//第一财产
新下载的JSON(HomeActivity.this).execute();//第二财产
新下载的JSON(HomeActivity.this).execute();//第三财产
新下载的JSON(HomeActivity.this).execute();//第四财产
公共类下载JSON扩展异步任务{
私有静态语境;
公共下载JSON(上下文){
this.context=上下文;
}
@凌驾
受保护的Void doInBackground(属性信息…参数){
试一试{
startWebServiceData(参数[0]);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
返回空值
}
void startWebServiceData(PropertyInfo propertyValue)引发IOException{
字符串URL=“xxxxxx”;
字符串SOAP_NAMESPACE=“xxxxx”;
字符串方法\u NAME=“xxxxx”;
字符串SOAP\u ACTION=“xxxxx”;
SoapObject SoapObject;
soapObject=新的soapObject(SOAP\名称空间、方法\名称);
addProperty(propertyValue);
SoapSerializationEnvelope envp=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envp.dotNet=true;
环境setOutputSoapObject(soapObject);
System.out.println(“soapObject==>”+envp.bodyOut);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
试一试{
调用(SOAP_操作,envp);
SoapPrimitive响应=(SoapPrimitive)envp.getResponse();
字符串结果=response.toString();
System.out.println(“来自服务器的响应数据===”+结果);
parseJson(结果);
}捕获(例外e){
}}
私有静态void parseJson(字符串结果){
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
} 

实例化它4次并实现多个回调?@SergiCastellsaguéMillán可以更具体一些。please@SergiCastellsaguéMillán我想这会减慢我的应用程序的速度。
newdosomethingasync1().execute();新的DoSomethingAsync2().execute();新的DoSomethingAsync3().execute();新的DoSomethingAsync4().execute()不会减慢任何速度,因为UI线程已用完。@SergiCastellsaguéMillán。请尝试一下,这是唯一的方法还是其他方法?这些参数是什么[0],请解释moreparams数组包含Url、命名空间、方法名和soap操作值。例如,params[0]url,名称空间等于params[1],方法名称params[3]和saop操作params[4]问题是url名称空间,soapaction对于所有Webercice调用都是相同的,只有propertyInfo值更改。propertyInfo值是什么?我将向我的SOAP请求propertyInfo pi=new propertyInfo()发送propertyInfo值;pi.setName(“isMonth”);pi.设定值(假);addProperty(pi);PropertyInfo pi2=新的PropertyInfo();pi2.setType(PropertyInfo.INTEGER_类);pi2.设置名称(“catgoryNumber”);pi2.设定值(1);soapObject.addProperty(pi2);