0

こんにちは、複数の webservices 呼び出しを並行して実行する方法を知りたいです。実際のシナリオは、アプリが最初に起動したときに、ユーザーがアプリを操作する前に、4 つの異なる webservices を呼び出してデータベースに入力する必要があることです。誰かがこれを行うための最良のアプローチを提案できますか? また、この 4 つの Web サービス呼び出しを実行するために AsyncTask を使用する方法。

今のところ、非同期タスクを使用してhomeActivtyで1つのWebサービス呼び出しを行っています

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 に答える