PI不能以xml开头

时间:2013-05-15 12:56:43

标签: java android xml

    //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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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

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

所以你的XML应该以

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

请参阅well-formed document

上的Wiki

答案 2 :(得分:0)

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

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

但我的客户端代码是:

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

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

(我将其用于J2ME sdk 3.0.5)

答案 3 :(得分:0)

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

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