2012-09-24 3 views
0

그래서 SOAP을 통해 Android 앱에서 액세스하려는 PHP 웹 서비스가 있습니다. 나는 PHP 클라이언트를 로컬로 구축하고 (온라인 인) 웹 서비스로부터 요청을 시도했기 때문에 웹 서비스가 제대로 작동하고 있다는 것을 알았습니다. 그리고 PHP 클라이언트는 올바른 데이터를 검색 할 수있었습니다. 문제는 봉투의 "getResponse()"를 시도 할 때 NullPointerException입니다. 또한 봉투의 "bodyIn"도 null입니다. 나는 HttpTransportSE의 responseDump를 시도하고 출력했으며 WSDL 파일 자체를 보여 주었다. WSDL 파일이 아닌 응답 엔벨로프를 반환하면 안됩니까?ksoap2 응답이 null입니다. responseDump는 봉투가 아닌 wsdl 파일입니다.

안드로이드 클라이언트 :

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는 웹 서비스 :

<?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 웹 사이트를 가리켜 야하는 몇 가지 해결책을 읽었습니다. rsdl 파일 대신 rvice. 나는 그것을 시도했다. 그리고 그것은 아직도 null이었다. responseDump를 검사했을 때 볼 수있는 것은 모두 특수 문자였습니다.

답변

1

나는 내 자신의 문제를 해결했습니다! 따라서 PHP 웹 서비스에 액세스 할 때 URL이 웹 서비스 자체를 가리켜 야하며 WSDL 파일이 아닌 일부 지침이 언급했듯이 다른 사람들이 정확하다는 것을 알게되었습니다. 지금은 이미 PHP 웹 서비스로 가리키는 시도했지만 오류가 여전히 대신 간단한의, 그 응답이 정수의 배열 때문에 ClassCastException이 발생했다 판명 된 남아 :

SoapObject response = (SoapObject)envelope.getResponse(); 
수행해야합니다 무엇

입니다 : 여기에 도착 사람들을 위해

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

젠장. 방금 Php-Url을 가리키는 2 일 간의 작업을 저장했습니다. 감사! – Ours

+0

잘 알고 있습니다. 나는 또한 SO 게시물을 통해 알게되었습니다. – Franz

1

온 예외가 있기 때문에 :

Log.e ("responseDump", transport.responseDump.toString는());

나 :

에서 System.out.println (transport.responseDump.toString());

당신은 당신이 문자열을 인쇄하기 전에 전화는 다음과 같이 구성되어 있는지 확인해야합니다 : 당신이 당신의 transport.debug = 사실이있는 경우

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

을;transport.call (SOAP_ACTION, envelope) 뒤에;, 충돌하고 문자열이 레코딩됩니다! 호출하기 전에 디버그를 true로 설정했는지 확인하십시오.

관련 문제