2010-12-14 69 views
1

自从最近三天以来,我一直在挖掘以找出使用kSoap2访问Web服务的适当方式。现在我能够访问Web服务,但是我需要知道,我是否遵循了正确的方式,或者我已经走出了标准。我已经发布了完整的代码和输出,如果我在任何地方出错了,请纠正我。在android中使用kSoap2访问WebService

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

} 
} 

输出:

Please click here to see the Output

任何帮助表示赞赏....在此先感谢

回答

2

根据输出,它看起来对我来说,你的web服务返回LogObject,这意味着它是一个复杂的对象。要处理复杂的对象,您还应该在Android中实现相同的类,并实现KSOAP Marshal接口。之后,您应该向该类注册添加映射,以便KSOAP知道如何处理接收到的对象。

有关如何使用复杂的对象与KSOAP在Android上工作的详细信息,请按照本教程:

KSOAP Android Web Service Tutorial with Sample Code on Complex Objects

+0

雅,这种方法对我帮助很大。谢谢很高兴帮助...我将发布我的代码一旦完成。非常感谢 – 2010-12-28 13:19:37

+0

我很高兴它帮助你 – 2011-01-06 10:54:29