2012-04-18 135 views
4

我想在我的android應用程序中創建一個SOAP客戶端。我已經使用php SOAP客戶端測試了服務器,並且它一直在工作。但在我的Android應用程序中,我仍然遇到異常。任何人都可以幫助我解決最新錯誤以及如何解決它?由於Android KSOAP2請求到PHP SOAP服務器

PHP

<?php 
class service 
{  
public function service() 
{ } 

public function login($nickname, $password) 
{ 

    $sql = "select nick from user 
    where 
    nick =\"$nick\" 
    and 
    pass = \"$password\""; 

    if (($result = $this->db->query($sql)) && ($result->num_rows == 1)) 
     return true; 
    else 
     return false; 
} 
} 

$server = new SoapServer(null, array(
'uri' => "urn://www.domain.cz", 
'soap_version' => SOAP_1_2) 
);    
$server->setClass("service");  
$server->handle(); 
?> 

的Android

private static String SOAP_ACTION = "http://www.domain.cz/server/server.php"; 
    private static String NAMESPACE = "urn://www.domain.cz"; 
    //need fix namespace to work, final solution 
    // private static String NAMESPACE = "http://www.domain.cz/server/"; 
    private static String METHOD_NAME = "login"; 
    private static String URL = "http://www.domain.cz"; 

public void Connect() 
{ 
    //Initialize soap request + add parameters 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    //Use this to add parameters 
    request.addProperty("nick","peter"); 
    request.addProperty("password","somepassword"); 

    //Declare the version of the SOAP request 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 
    envelope.setOutputSoapObject(request); 

    //Needed to make the internet call 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    try { 
      //this is the actual part that will call the webservice 
      androidHttpTransport.call(SOAP_ACTION, envelope);//HERE is xmlpullparserexception after a while 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 

    // Get the SoapResult from the envelope body. 
    SoapObject result = (SoapObject)envelope.bodyIn; 

    if(result != null){ 
      TextView t = (TextView)this.findViewById(R.id.resultbox); 

      //Get the first property and change the label text 
      t.setText("SOAP response:\n\n" + result.getProperty(0).toString()); 
    } 

} 
+1

什麼是你所得到的_exact_例外呢? – 2012-04-18 18:34:55

回答

2

您的網址應該是SOAP端點,即http://www.domain.cz/server/server.php

你SOAP_ACTION應該包含方法名,即http://www.domain.cz/server/server.php/login

0

您正在使用KSOAP(?)我推測。我認爲你需要將URL指向Web服務路徑。我的URL沒有看到WSDL。

+0

是的,我正在使用KSOAP。我認爲我可以在沒有WSDFL文件的情況下使用它。如果KSOAP無法在沒有WSDL的情況下工作,那麼還有其他方法嗎? – 2012-04-18 14:57:12