1

Web サービスにアクセスするために kSoap2 を使用する適切な方法を見つけるために、過去 3 日間から掘り下げてきました。これで Web サービスにアクセスできるようになりましたが、正しい方法に従ったのか、標準から外れているのかを知る必要があります。取得した完全なコードと出力を投稿しました。どこかで間違っている場合は、修正してください。

// WebServiceConsumer.java

 public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException {

    /** Construction of the SoapObject */
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
    /** passing the values in to the webservice*/
    request.addProperty("iTopN", "0"); //variable name, value. got the variable name, from the wsdl file!

    /** Creation of the SoapEnvelope with the appropriate version*/
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);  //prepare request
    /** Creating AndroidTransport for passing the request to the URL where the service is located*/
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);  
    httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was (httpTransport.requestDump)
    httpTransport.call(SOAP_ACTION, envelope); //send request
    SoapObject result =(SoapObject)envelope.bodyIn; //get response
    return result;
 }

 public void callService() {
     try{
         SoapObject result = soap(METHOD_NAME, SOAP_ACTION, NAMESPACE, URL);
         Log.i(TAG,"Result:" + result);
         try {
            // FootballScoreParser.parseBusinessObject(result.getProperty(0).toString(), footballscore);
                 SoapObject logObject = (SoapObject) result.getProperty(0);
                 Log.i(TAG,"LogObject : " + logObject);
                 for(int i = 0; i < 10 ; i++) {
                 SoapObject logger = (SoapObject) logObject.getProperty(i);
                // Log.i(TAG,"Name : " + logger.getProperty("sName"));
                // Log.i(TAG,"Goals : "+ logger.getProperty("iGoals"));

                 /** Appending the sName,iGoals in to ArrayList name */
                    name.add((String)logger.getProperty("sName").toString());
                    goals.add((String) logger.getProperty("iGoals").toString());
                    country.add((String) logger.getProperty("sCountry").toString());
                    flag.add((String) logger.getProperty("sFlag").toString());

                 /** Converting the ArrayList into the Object Array*/
                    objName = name.toArray();
                    objGoals = goals.toArray();
                    objCountry = country.toArray();
                    objFlags = flag.toArray();
                 }
                 for(int j = 0; j < objName.length; j++){
                            Log.i(TAG,"Name ["+ j + "]=" + objName[j].toString() + "," + "Goals ["+ j + "]=" + objGoals[j].toString()+ ","  + "Country[" + j  + "]=" + objCountry[j].toString() + "," +"Flag[" +j+ "]=" + objFlags[j].toString());

                         }
                 }
         catch(Exception err){
             Log.i(TAG, "" + err);
         }


         }
         catch(Exception err){
             Log.i(TAG,""+ err);
         }
           /* catch(NumberFormatException err){
            err.printStackTrace();  
            }
            catch(IllegalArgumentException err){
                err.printStackTrace();
            }
            catch(IllegalAccessException err){
                err.printStackTrace();
            }
            catch(InstantiationException err){
                err.printStackTrace();
            }*/
         //}

// FootBallScrorerActivity.java

package com.project.mobile.FootballScorers;

import android.app.Activity;
import android.os.Bundle;

public class FootbalScorerActivity extends Activity {
WebServiceConsumer webconsumer;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webconsumer = new WebServiceConsumer();
    webconsumer.callService();

}
}

出力:

出力を表示するには、ここをクリックしてください

どんな助けでも感謝します....事前に感謝します

4

1 に答える 1

2

According to the output, it looks to me like your web service returns LogObject, which means it is a complex object. To work with complex objects, you should implement the same class in Android as well, and implement the KSOAP Marshal interface. After that you should register Add mapping to that class so that KSOAP knows how to handle the received object.

For more information on how to work with complex objects with KSOAP on Android, follow this tutorial:

KSOAP Android Web Service Tutorial with Sample Code on Complex Objects

于 2010-12-17T17:30:27.823 に答える