2012-09-24 55 views
0

所以我有一個PHP Web服務,我想通過SOAP從Android應用程序訪問。我確信Web服務正常運行,因爲我在本地構建了一個PHP客戶端,並試圖從Web服務請求(在線),並且PHP客戶端能夠檢索到正確的數據。當我嘗試信封的「getResponse()」時,問題是NullPointerException。此外,信封的「bodyIn」也是空的。我嘗試並輸出HttpTransportSE的responseDump,它顯示了WSDL文件本身。它不應該返回一個響應信封,而不是WSDL文件?ksoap2響應爲空。 responseDump是wsdl文件本身,不是信封

Android客戶端:

package com.franzsarmiento.shorecloudsurvey; 

import java.io.IOException; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.SoapFault; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import org.xmlpull.v1.XmlPullParserException; 

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

public class MainMenu extends Activity { 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_menu); 

     new DownloadNewSurveys().execute(); 
    } 

    private class DownloadNewSurveys extends AsyncTask<Void,Void,Void> { 

     private final static String NAMESPACE = "http://203.177.73.175:8080/"; 
     private final static String METHOD_NAME = "getSid"; 
     private final static String SOAP_ACTION = "http://203.177.73.175:8080/getSid"; 
     private final static String URL = "http://203.177.73.175:8080/sid.wsdl"; 

     @Override 
     protected Void doInBackground(Void... arg0) { 

      SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME); 

      SoapSerializationEnvelope envelope = new 
        SoapSerializationEnvelope(SoapEnvelope.VER11);   
      envelope.setOutputSoapObject(request); 

      HttpTransportSE ht = new HttpTransportSE(URL); 
      ht.debug = true; 

      try { 
       ht.call(SOAP_ACTION, envelope); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (XmlPullParserException e) { 
       e.printStackTrace(); 
      } 

      SoapObject response = null; 

      System.out.println(ht.responseDump); 

      try { 
       response = (SoapObject)envelope.getResponse(); 
      } catch (SoapFault e) { 
       response = (SoapObject)envelope.bodyIn; 
      } 

      System.out.println(response); 
      return null; 
     } 

    } 

} 

WSDL文件(這也是我得到同樣的事情時,我打印responseDump):

<definitions name='Sid' 

    targetNamespace='http://203.177.73.175:8080/' 

    xmlns:tns='http://203.177.73.175:8080/' 

    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 

    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 

    xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 

    xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 

    xmlns='http://schemas.xmlsoap.org/wsdl/'> 

<message name='getSidRequest'/> 

<message name='getSidResponse'> 

    <part name='sidArray' type='tns:ArrayOfinteger[]'/> 

</message> 

<portType name='SidPortType'> 

    <operation name='getSid'> 

    <input message='tns:getSidRequest'/> 

    <output message='tns:getSidResponse'/> 

    </operation> 

</portType> 

<binding name='SidBinding' type='tns:SidPortType'> 

    <soap:binding style='rpc' 

    transport='http://schemas.xmlsoap.org/soap/http'/> 

    <operation name='getSid'> 

    <soap:operation soapAction='getSid'/> 

    <input> 

     <soap:body use='encoded' namespace='urn:sid' 

     encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 

    </input> 

    <output> 

     <soap:body use='encoded' namespace='urn:sid' 

     encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 

    </output> 

    </operation> 

</binding> 

<service name='SidService'> 

    <port name='SidPort' binding='SidBinding'> 

    <soap:address location='http://203.177.73.175:8080/sid_server.php'/> 

    </port> 

</service> 

</definitions> 

PHP的Web服務:

<?php 

function getSid() { 
    $con = mysql_connect('localhost','fsarmiento','shorecloud') or die (mysql_error()); 
    $db = mysql_select_db('limesurvey',$con); 

    $query = ' 
     SELECT 
      sid 
     FROM 
      lime_surveys 
    ;'; 

    $result = mysql_query($query) or die (mysql_error()); 

    $arrSid = array(); 

    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
     $arrSid[] = intval($row['sid']); 
    } 

    return $arrSid; 

} 

ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache 

$server = new SoapServer("sid.wsdl", array('encoding'=>'UTF-8')); 

$server->addFunction("getSid"); 

$server->handle(); 

?> 

此外,我已經閱讀了一些解決方案,該URL應該指向PHP網頁服務而不是wsdl文件。我試過了,它仍然是空的。雖然當我檢查responseDump時,我看到的只是特殊字符。

回答

1

我解決了我自己的問題!所以事實證明,其他人在訪問PHP Web服務時是正確的,因此URL必須指向Web服務本身,而不是像某些指南中提到的那樣指向WSDL文件。現在,我已經試了一下指向PHP Web服務,但仍然存在錯誤,它原來是由一個ClassCastException,因爲反應是整數數組引起的,所以不是簡單:

SoapObject response = (SoapObject)envelope.getResponse(); 

什麼是必須做的是:

Vector<SoapObject> response = (Vector<SoapObject>)envelope.getResponse(); 
+0

該死的男人。你剛剛救了我2天的工作,指出了Php-Url的事情。謝謝! – Ours

+0

很高興知道。我也通過SO帖子找到了。 – Franz

1

對於那些誰也來到這裏,因爲有你的一個例外:

Log.e( 「responseDump」,transport.responseDump.toString());

或您:

的System.out.println(transport.responseDump.toString());

你必須確保你的電話的結構像這樣在打印字符串之前:

HttpTransportSE transport = new HttpTransportSE(URL); 
transport.debug = true; 
transport.call(SOAP_ACTION, envelope); 

如果您有transport.debug = TRUE;之後transport.call(SOAP_ACTION,envelope);,你會崩潰,你的字符串會燃燒!確保在調用之前將調試設置爲true ...