Android Ksoap2 的 Web 服务问题

发布于 2024-11-06 02:00:18 字数 9408 浏览 1 评论 0原文

我正在开发一个使用 Ksaop2 和 Android 2.3.3 的应用程序,但出现以下错误,有人可以帮忙吗?提前致谢。

WARN/System.err(591): org.xmlpull.v1.XmlPullParserException: 意外类型 (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@4052fba0)

这是我的代码:

package test.zlu.opo;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class StockQuoteService extends Activity {

private static final String NAMESPACE = "http://www.restfulwebservices.net/ServiceContracts/2008/01";

private static String URL = "http://www.restfulwebservices.net/wcf/StockQuoteService.svc?wsdl";

private static final String METHOD_NAME = "GetStockQuote";

private static String SOAP_ACTION = "GetStockQuote";


private String stockQuote;

private Button okButton;
private EditText textInput;
private ImageView imageView1;
private TextView textStockQuote;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    okButton = (Button)  findViewById(R.id.stockQuery);
    textInput = (EditText) findViewById(R.id.textStockCode);
    imageView1 = (ImageView) findViewById(R.id.ImageView01);
    textStockQuote = (TextView) findViewById(R.id.StockQuote);

    okButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            showStockQuote();
        }
   });
}

private void showStockQuote() {
    String stockCode = textInput.getText().toString();
    if (stockCode.length() == 0) stockCode = "";
    getStockQuote(stockCode);

    textStockQuote.setText(getStockQuote());
    //imageView1.setImageResource(getIconToday(0));
}

public void getStockQuote(String stockCode) {
    try {
        SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);

        rpc.addProperty("parameters", stockCode);

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

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.bodyOut = rpc;
        //envelope.dotNet= true;
        envelope.setOutputSoapObject(rpc);

        ht.call(SOAP_ACTION, envelope);

        SoapObject result = (SoapObject) envelope.bodyIn;
        System.out.println(result.toString());


    } catch (Exception e) {
        e.printStackTrace();
    }
}



private void parseResult(SoapObject detail) throws UnsupportedEncodingException{
    stockQuote = detail.getProperty(0).toString();

}


private String getStockQuote() {
    return stockQuote;
}
/*
private int getIconToday(int index) {
     return 1;
}*/
}

这是 WSDL:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="StockQuoteService"
targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
xmlns:tns="http://www.restfulwebservices.net/ServiceContracts/2008/01"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract"
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:wsa10="http://www.w3.org/2005/08/addressing"
xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
  <wsdl:types>
    <xsd:schema targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01/Imports">

      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd0"
      namespace="http://www.restfulwebservices.net/ServiceContracts/2008/01" />
      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd3"
      namespace="http://GOTLServices.FaultContracts/2008/01" />
      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd1"
      namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd2"
      namespace="http://www.restfulwebservices.net/DataContracts/2008/01" />
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="IStockQuoteService_GetStockQuote_InputMessage">

    <wsdl:part name="parameters" element="tns:GetStockQuote" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetStockQuote_OutputMessage">

    <wsdl:part name="parameters"
    element="tns:GetStockQuoteResponse" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetStockQuote_DefaultFaultContractFault_FaultMessage">

    <wsdl:part name="detail" element="q1:DefaultFaultContract"
    xmlns:q1="http://GOTLServices.FaultContracts/2008/01" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetWorldMajorIndices_InputMessage">

    <wsdl:part name="parameters"
    element="tns:GetWorldMajorIndices" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetWorldMajorIndices_OutputMessage">

    <wsdl:part name="parameters"
    element="tns:GetWorldMajorIndicesResponse" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetWorldMajorIndices_DefaultFaultContractFault_FaultMessage">

    <wsdl:part name="detail" element="q2:DefaultFaultContract"
    xmlns:q2="http://GOTLServices.FaultContracts/2008/01" />
  </wsdl:message>
  <wsdl:portType name="IStockQuoteService">
    <wsdl:operation name="GetStockQuote">
      <wsdl:input wsaw:Action="GetStockQuote"
      message="tns:IStockQuoteService_GetStockQuote_InputMessage" />
      <wsdl:output wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetStockQuoteResponse"
      message="tns:IStockQuoteService_GetStockQuote_OutputMessage" />
      <wsdl:fault wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetStockQuoteDefaultFaultContractFault"
      name="DefaultFaultContractFault"
      message="tns:IStockQuoteService_GetStockQuote_DefaultFaultContractFault_FaultMessage" />
    </wsdl:operation>
    <wsdl:operation name="GetWorldMajorIndices">
      <wsdl:input wsaw:Action="GetWorldMajorIndices"
      message="tns:IStockQuoteService_GetWorldMajorIndices_InputMessage" />
      <wsdl:output wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetWorldMajorIndicesResponse"
      message="tns:IStockQuoteService_GetWorldMajorIndices_OutputMessage" />
      <wsdl:fault wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetWorldMajorIndicesDefaultFaultContractFault"
      name="DefaultFaultContractFault"
      message="tns:IStockQuoteService_GetWorldMajorIndices_DefaultFaultContractFault_FaultMessage" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="BasicHttpBinding_IStockQuoteService"
  type="tns:IStockQuoteService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetStockQuote">
      <soap:operation soapAction="GetStockQuote"
      style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="DefaultFaultContractFault">
        <soap:fault name="DefaultFaultContractFault"
        use="literal" />
      </wsdl:fault>
    </wsdl:operation>
    <wsdl:operation name="GetWorldMajorIndices">
      <soap:operation soapAction="GetWorldMajorIndices"
      style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="DefaultFaultContractFault">
        <soap:fault name="DefaultFaultContractFault"
        use="literal" />
      </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="StockQuoteService">
    <wsdl:port name="BasicHttpBinding_IStockQuoteService"
    binding="tns:BasicHttpBinding_IStockQuoteService">
      <soap:address location="http://www.restfulwebservices.net/wcf/StockQuoteService.svc" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

I am developing an application which use Ksaop2 and Android 2.3.3 but I get the below error, can anyone help on this? thanks in advance.

WARN/System.err(591): org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@4052fba0)

Here is my code:

package test.zlu.opo;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class StockQuoteService extends Activity {

private static final String NAMESPACE = "http://www.restfulwebservices.net/ServiceContracts/2008/01";

private static String URL = "http://www.restfulwebservices.net/wcf/StockQuoteService.svc?wsdl";

private static final String METHOD_NAME = "GetStockQuote";

private static String SOAP_ACTION = "GetStockQuote";


private String stockQuote;

private Button okButton;
private EditText textInput;
private ImageView imageView1;
private TextView textStockQuote;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    okButton = (Button)  findViewById(R.id.stockQuery);
    textInput = (EditText) findViewById(R.id.textStockCode);
    imageView1 = (ImageView) findViewById(R.id.ImageView01);
    textStockQuote = (TextView) findViewById(R.id.StockQuote);

    okButton.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            showStockQuote();
        }
   });
}

private void showStockQuote() {
    String stockCode = textInput.getText().toString();
    if (stockCode.length() == 0) stockCode = "";
    getStockQuote(stockCode);

    textStockQuote.setText(getStockQuote());
    //imageView1.setImageResource(getIconToday(0));
}

public void getStockQuote(String stockCode) {
    try {
        SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);

        rpc.addProperty("parameters", stockCode);

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

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.bodyOut = rpc;
        //envelope.dotNet= true;
        envelope.setOutputSoapObject(rpc);

        ht.call(SOAP_ACTION, envelope);

        SoapObject result = (SoapObject) envelope.bodyIn;
        System.out.println(result.toString());


    } catch (Exception e) {
        e.printStackTrace();
    }
}



private void parseResult(SoapObject detail) throws UnsupportedEncodingException{
    stockQuote = detail.getProperty(0).toString();

}


private String getStockQuote() {
    return stockQuote;
}
/*
private int getIconToday(int index) {
     return 1;
}*/
}

And here is the WSDL:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions name="StockQuoteService"
targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
xmlns:tns="http://www.restfulwebservices.net/ServiceContracts/2008/01"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract"
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:wsa10="http://www.w3.org/2005/08/addressing"
xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex">
  <wsdl:types>
    <xsd:schema targetNamespace="http://www.restfulwebservices.net/ServiceContracts/2008/01/Imports">

      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd0"
      namespace="http://www.restfulwebservices.net/ServiceContracts/2008/01" />
      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd3"
      namespace="http://GOTLServices.FaultContracts/2008/01" />
      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd1"
      namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
      <xsd:import schemaLocation="http://www.restfulwebservices.net/wcf/StockQuoteService.svc?xsd=xsd2"
      namespace="http://www.restfulwebservices.net/DataContracts/2008/01" />
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="IStockQuoteService_GetStockQuote_InputMessage">

    <wsdl:part name="parameters" element="tns:GetStockQuote" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetStockQuote_OutputMessage">

    <wsdl:part name="parameters"
    element="tns:GetStockQuoteResponse" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetStockQuote_DefaultFaultContractFault_FaultMessage">

    <wsdl:part name="detail" element="q1:DefaultFaultContract"
    xmlns:q1="http://GOTLServices.FaultContracts/2008/01" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetWorldMajorIndices_InputMessage">

    <wsdl:part name="parameters"
    element="tns:GetWorldMajorIndices" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetWorldMajorIndices_OutputMessage">

    <wsdl:part name="parameters"
    element="tns:GetWorldMajorIndicesResponse" />
  </wsdl:message>
  <wsdl:message name="IStockQuoteService_GetWorldMajorIndices_DefaultFaultContractFault_FaultMessage">

    <wsdl:part name="detail" element="q2:DefaultFaultContract"
    xmlns:q2="http://GOTLServices.FaultContracts/2008/01" />
  </wsdl:message>
  <wsdl:portType name="IStockQuoteService">
    <wsdl:operation name="GetStockQuote">
      <wsdl:input wsaw:Action="GetStockQuote"
      message="tns:IStockQuoteService_GetStockQuote_InputMessage" />
      <wsdl:output wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetStockQuoteResponse"
      message="tns:IStockQuoteService_GetStockQuote_OutputMessage" />
      <wsdl:fault wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetStockQuoteDefaultFaultContractFault"
      name="DefaultFaultContractFault"
      message="tns:IStockQuoteService_GetStockQuote_DefaultFaultContractFault_FaultMessage" />
    </wsdl:operation>
    <wsdl:operation name="GetWorldMajorIndices">
      <wsdl:input wsaw:Action="GetWorldMajorIndices"
      message="tns:IStockQuoteService_GetWorldMajorIndices_InputMessage" />
      <wsdl:output wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetWorldMajorIndicesResponse"
      message="tns:IStockQuoteService_GetWorldMajorIndices_OutputMessage" />
      <wsdl:fault wsaw:Action="http://www.restfulwebservices.net/ServiceContracts/2008/01/IStockQuoteService/GetWorldMajorIndicesDefaultFaultContractFault"
      name="DefaultFaultContractFault"
      message="tns:IStockQuoteService_GetWorldMajorIndices_DefaultFaultContractFault_FaultMessage" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="BasicHttpBinding_IStockQuoteService"
  type="tns:IStockQuoteService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetStockQuote">
      <soap:operation soapAction="GetStockQuote"
      style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="DefaultFaultContractFault">
        <soap:fault name="DefaultFaultContractFault"
        use="literal" />
      </wsdl:fault>
    </wsdl:operation>
    <wsdl:operation name="GetWorldMajorIndices">
      <soap:operation soapAction="GetWorldMajorIndices"
      style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
      <wsdl:fault name="DefaultFaultContractFault">
        <soap:fault name="DefaultFaultContractFault"
        use="literal" />
      </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="StockQuoteService">
    <wsdl:port name="BasicHttpBinding_IStockQuoteService"
    binding="tns:BasicHttpBinding_IStockQuoteService">
      <soap:address location="http://www.restfulwebservices.net/wcf/StockQuoteService.svc" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文