2012-08-15 01:42
浏览 58

如何使用kso​​ap2-android正确调用我的PHP Web服务?

So far I have created a PHP client, and a VB.NET client which both successfully call my PHP web-service. To get the latter to work I needed to use the SoapUI tool from SourceForge. It told me that my wsdl was not WS-I compliant. I did not need the Pro version to test my service interactively as it allows you to directly edit the soap request. After fixing my WSDL and getting my VB.Net client functioning android is still a problem.

I also attached the source code for ksoap2-andriod so that I could step through while debugging. It helped a little but there are bundled dependencies for which the source is not included, in particular "kxml2 v1.6". If anyone can point me to a source zip or Jar for that I'd appreciate it.

This is the error I cant get past when calling the PHP webservice from my android client.

  org.xmlpull.v1.XmlPullParserException: expected: START_TAG   {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <definitions name='naturallyIrrationalsoapserver' targetNamespace='http://www.naturallyIrrational.com'>@10:42 in java.io.InputStreamReader@44dce560)

Its telling me it cant parse the WSDL\XML - the poistion @10.42 is the end of the opening definitions tag.

I believe that as the WSDL in now WS-I compliant that the problem is withing this service namespace definitions as interpreted by Ksoap2. Here is my android client code used to call it.

public class SoapClientActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        TextView tv = (TextView) findViewById(R.id.myText);
        String soapResponse="";
        final String METHOD_NAME = "getArrval";
        final String SOAP_ACTION = "http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.wsdl";
        final String NAMESPACE = "http://www.naturallyIrrational.com";

* This next line was incorrect and should point to http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.php *

        final String URL = "http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.wsdl";

        if (InternetStatus.getInstance(this).isOnline(this)) {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                    PropertyInfo pi = new PropertyInfo();

                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
                    HttpTransportSE andHttpTransport = new HttpTransportSE(URL); 
                    andHttpTransport.debug = true;
                    andHttpTransport.call(SOAP_ACTION, envelope);

* there is something wrong with returning this value, it has been received but throws an error when passed back from ksoap *

 soapResponse = (String) envelope.getResponse();  //Exception is thrown here  
                        String strrequest = andHttpTransport.requestDump;
                        String strresponse = andHttpTransport.responseDump;

            }catch(Exception e){soapResponse = "Nope not working "+"

" + e.getMessage() + "/n/n" + e.getStackTrace() ;}
            } else {soapResponse="You are not online"; }   


If I am doing something dumb and some one can point it out I'd appreciate it.

Is there a directive to not cache and reuse wsdl in Android like there is in PHP?

Maybe using net beans will help, that's next when I have time. If anyone can please help in the interim, don't hesitate to suggest something.

class naturallyIrrational {
  private $arrval = array("pi" => 3.1415,"e" =>  2.7183, "rt2" => 1.414, "phi" => 1.618 );  
  function getIrrationalvalue($valname) {
$myFile = "logFile.html";
$fh = fopen($myFile, 'a') or die("can't open file");
$datq = date('m/d/Y h:i:s a', time());
fwrite($fh, $datq);
if (isset($this->arrval[$valname])) {

$stringData = " ".$valname." = ".$this->arrval[$valname]."<br/>";
fwrite($fh, $stringData);
return $this->arrval[$valname];
    } else {
    throw new SoapFault("Server","Unknown Symbol '$valname'.");
$server = new SoapServer("naturallyIrrational.wsdl");
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwen1915
    douwen1915 2012-08-20 03:26

    I discovered that the URL string needs to point at the public php.

    final String URL = "http://www.naturallyIrrational.com/naturallyIrrationalsoapserver.php";

    Really a very simple thing in the end after 3 weeks. Downloading the source code was essential as it allowed me to see the service is functioning. There was/is another issue regarding passing parameters, like this:

    PropertyInfo pi = new PropertyInfo();

    but that is a different question.

    Thank you for your comments.

    点赞 评论
  • dongyi2889
    dongyi2889 2012-08-15 15:02

    I reacently developed a Restful web service. The was many reasons to develop the web service as a restful other than the SOAP. I did a very small project on SOAP just to get the big picture then moved to develop on restful as it is more advantages. To develop both models Netbeans IDE provide very convinient facilities. Read Web Services Learning Trail

    That link contain both restful and SOAP/WDSL web service development in very easy way...Hope this would help.

    点赞 评论