2013-04-23 56 views
0

我想使用SOAP連接到Webside並以JSON格式返回結果。不過我關於這個問題真的很黑,真的需要一些指導..Android:訪問SOAP Webside返回JSON

我SoapAccess代碼:

public SoapAccess(){ 
     this.user_id = "MYID"; 
     this.password = "MYPASSWORD"; 
     this.busstop_id = "16010366"; 


    Thread networkThread = new Thread() { 
    @Override 
    public void run() { 
      try { 

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

    request.addProperty("user", user_id); 
    request.addProperty("password",password); 
    request.addPropertyIfValue("busStopId", busstop_id); 

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

    // Make the soap call. 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     androidHttpTransport.call(SOAP_ACTION, envelope);  

     SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 
     Log.i("myapp",result.toString()); 
     System.out.println(" --- response ---- " + result); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 

      } 
     }; 
    networkThread.start(); 
    } 

肥皂看起來像這樣:

POST /InfoTransit/userservices.asmx HTTP/1.1 
Host: 10.0.2.52 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://miz.it/infotransit/getUserRealTimeForecastByStop" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <getUserRealTimeForecastByStop xmlns="http://miz.it/infotransit"> 
     <auth> 
     <user>string</user> 
     <password>string</password> 
     </auth> 
     <handShakeUser> 
     <userIdentifier>string</userIdentifier> 
     <token>string</token> 
     <response>NONE or Success or GenericError or NotValidToken or NoExistingUser or WsAuthenticationError</response> 
     </handShakeUser> 
     <busStopId>string</busStopId> 
    </getUserRealTimeForecastByStop> 
    </soap:Body> 
</soap:Envelope> 
HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <getUserRealTimeForecastByStopResponse xmlns="http://miz.it/infotransit"> 
     <getUserRealTimeForecastByStopResult>string</getUserRealTimeForecastByStopResult> 
    </getUserRealTimeForecastByStopResponse> 
    </soap:Body> 
</soap:Envelope> 

系統LogCat中的錯誤:

04-23 17:31:50.819: W/System.err(11156): SoapFault - faultcode: 'soap:Server' faultstring: 'System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
04-23 17:31:50.819: W/System.err(11156): at UserServices.isWsAuthenticatedPUB(WsAuthentication _wsAuth) in c:\DISK_E\SAE\Infotransit\App_Code\UserServices.cs:line 609 
04-23 17:31:50.819: W/System.err(11156): at UserServices.getUserRealTimeForecastByStop(WsAuthentication auth, HandShake handShakeUser, String busStopId) in c:\DISK_E\SAE\Infotransit\App_Code\UserServices.cs:line 460 
04-23 17:31:50.819: W/System.err(11156): --- End of inner exception stack trace ---' faultactor: 'null' detail: [email protected] 
04-23 17:31:50.819: W/System.err(11156): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:141) 
04-23 17:31:50.823: W/System.err(11156): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140) 
04-23 17:31:50.823: W/System.err(11156): at org.ksoap2.transport.Transport.parseResponse(Transport.java:116) 
04-23 17:31:50.823: W/System.err(11156): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:259) 
04-23 17:31:50.823: W/System.err(11156): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114) 
04-23 17:31:50.823: W/System.err(11156): at com.mso.master.SoapAccess$1.run(SoapAccess.java:58) 

有沒有人可以給我一個關於如何連接到這個網頁並獲得一些結果的精確位置?

回答

0

如果您只是想連接到網站並獲取內容(JSON),我會推薦使用java httpconnection類。您可以獲取文件內容的輸入流。

+0

網頁上有代碼段的語氣 – Guru 2013-04-24 00:38:45