1
    //Initialize soap request 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    //Use this to add parameters
    request.addProperty("agentcode",agentCode);
    request.addProperty("pincode",agentCodePin);
    request.addProperty("appversion",appversion);
    request.addProperty("deviceid",deviceid);
    request.addProperty("latitude",latitude);
    request.addProperty("longitude",longitude);

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

    envelope.setOutputSoapObject(request);

    envelope.dotNet = true;

    try {

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        //this is the actual part that will call the web service
        androidHttpTransport.call(SOAP_ACTION, envelope);

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


        if(result != null)
        {

             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             factory.setNamespaceAware(true);

             DocumentBuilder builder = factory.newDocumentBuilder();

             InputSource is = new InputSource();
             is.setCharacterStream(new StringReader( result.toString()));

             org.w3c.dom.Document doc = builder.parse(is);

             doc.getDocumentElement().normalize();
            //Get Node List
             NodeList nlist = doc.getElementsByTagName("paypoint");
            //Get node
             Node nNode = (Node) nlist.item(0);

            if (nNode.getType(0) == Node.ELEMENT)
            {
                //get element
                Element elt = (Element) nNode; 

                // Get token
                this.token = elt.getElementsByTagName("token").item(0).getTextContent();
                // Get flag
                this.flag = elt.getElementsByTagName("statusCode").item(0).getTextContent();
                // Get agent name
                this.agentName = elt.getElementsByTagName("fullname").item(0).getTextContent();
            }

            }
          } catch (Exception e)
          {
                throw e;
          }

        return this.flag;

我在这一行遇到问题:is.setCharacterStream(new StringReader(result.toString()));

错误是:PI 不能以 xml 开头(部分:java.io.stringReader@40579f48 中的未知 xml@1:30)

我的 XML 文件如下所示:

<string xmlns="http://tempuri.org/">
<?xml version="1.0" encoding="utf-16"?><paypoint> <token>PkSMTTulAndNmM9R4Vmi+QRWtChW/Xs61sPERoTpB5eEgRfrQKUi6r2rqLQNusvJpVJ1oZBc8Z0=</token>   <statusCode>1</statusCode><statusText>VALID USER</statusText><fullname>Dao    Lacina</fullname><walletbalance>2000.00</walletbalance></paypoint>

任何帮助

4

4 回答 4

4

<?xml version="1.0" encoding="utf-16"?>必须是 XML 文档的第一行。你可以试试吗:

<?xml version="1.0" encoding="utf-16" ?>
<string xmlns="http://tempuri.org/">
<paypoint> 
<token>PkSMTTulAndNmM9R4Vmi+QRWtChW/Xs61sPERoTpB5eEgRfrQKUi6r2rqLQNusvJpVJ1oZBc8Z0=</token>   
<statusCode>1</statusCode>
<statusText>VALID USER</statusText>
<fullname>Dao    Lacina</fullname>
<walletbalance>2000.00</walletbalance>
</paypoint>
</string>
于 2013-05-15T13:13:38.540 回答
1

您的 XML 不是well-formed. 第一行必须是

<?xml version="1.0" encoding="utf-16"?>

所以你的 XML 应该以

<?xml version="1.0" encoding="utf-16"?>
<string xmlns="http://tempuri.org/">

在格式良好的文档上查看 Wiki

于 2013-05-15T13:15:13.183 回答
1

这发生在我的 android 在 Eclipse 中...通过从文件开头删除 3 字节 utf-8 bom 解决了它。

http://en.wikipedia.org/wiki/Byte_order_mark#UTF-8

于 2015-04-02T01:47:19.060 回答
0

我也有同样的问题,但我发现这不是格式不正确的 XML 文档的错,有时会是那个问题,但这次是由于没有使用服务器用来发送的正确输出流数据到客户端......我的服务器端代码是这样的:

DataOutputStream dos = new DataOutputStream((OutputStream) response.getOutputStream());

但我的客户端代码是:

InputStream is =(InputStream) httpConnection.openDataInputStream();

所以最后我改变了流以相互匹配,然后问题也解决了......我仍然不明白为什么当这些流不同时会发生这种情况,即使流是由同一个 InputStream 继承的

(我将它用于 J2ME sdk 3.0.5)

于 2013-08-11T17:44:24.240 回答