Android xml响应说utf8和转换错误

时间:2015-08-22 17:50:29

标签: android xml encoding utf-8

我通过url将参数发送到服务器上的php脚本,并返回xml响应。我已经确保将我的网址中的所有参数编码为utf8。

HttpClient httpClient = new DefaultHttpClient(httpParameters);

HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);

HttpEntity httpEntity = httpResponse.getEntity();

entityResponse = EntityUtils.toString(httpEntity);

return entityResponse;

当xml返回此特定脚本时,我发现这是一个响应。

<b>Warning</b>:  DOMElement::setAttribute() [<a href='domelement.setattribute'>domelement.setattribute</a>]: string is not in UTF-8 in <b>(server_address)/getmessagesandroidc.php</b> on line <b>21</b><br />
<br />
<b>Warning</b>:  DOMDocument::saveXML() [<a href='domdocument.savexml'>domdocument.savexml</a>]: output conversion failed due to conv error, bytes 0xF0 0x9F 0x91 0x22 in <b>(server_address)/getmessagesandroidc.php</b> on line <b>33</b><br />
<?xml version="1.0" encoding="ISO-8859-1"?>
<loginbrrr><loginbrrr...

这里我感到很困惑。它在php脚本中引用的特定行是获取用户发布的消息列表,这些消息都是utf8格式。

我已经缩小了问题范围。当我将返回的消息数限制为25时,该脚本实际上有效。但是如果我将数量增加到26或更高,脚本会立即给我这个错误。我已经尝试更改消息26,以确定是否是导致错误的消息,但事实并非如此,我几乎可以肯定它与消息的长度有关。

我在我的应用程序中运行了很多其他脚本,其中一些脚本甚至返回的xml响应比这个更长。所以我很困惑,为什么长度会给我这些特定的错误。

当我尝试解析xml字符串时,我得到了日志错误

org.w3c.dom.DOMException: Only one root element allowed
        at org.apache.harmony.xml.dom.DocumentImpl.insertChildAt(DocumentImpl.java:421)
        at org.apache.harmony.xml.dom.InnerNodeImpl.appendChild(InnerNodeImpl.java:52)
        at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:306)
        at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:128)
        at com.packagename.XmlValues.getDomElement(XmlValues.java:72)

我在第72行结束的XMLValues的代码是

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
is.setEncoding("UTF-8");
doc = db.parse(is);      //line 72

&#39; xml&#39;变量是从上面的代码返回的实体响应。

任何帮助都会非常感激,因为我已经为此工作了几天。

编辑:这里也是剧本。

<?php
$login = $_GET["frcode"];

$iter = "Nationals";

$aj = "localhost";
$bj = "...";
$username_Database = "...";
$password_Database = "...";
$dbh = new PDO("mysql:host=$aj;dbname=$bj", $username_Database,     $password_Database);
$sql = 'SELECT frcode, snam, autoinc, importantz, messagez, da, mont, yea,  comit FROM chatstream WHERE frcode = ? AND length(messagez) > 0 ORDER BY autoinc DESC';
$params = array( $login );
$q = $dbh->prepare( $sql );
$q->execute( $params );
$doc = new DOMDocument();
$r = $doc->createElement("loginbrrr" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
     $e = $doc->createElement( "loginbrrr" );
     $e->setAttribute( 'snam', $row['snam'] );
     $e->setAttribute( 'messagez', $row['messagez'] );
     $e->setAttribute( 'frcode', $row['frcode'] );
     $e->setAttribute( 'importantz', $row['importantz'] );
     $e->setAttribute( 'autoinc', $row['autoinc'] );
     $e->setAttribute( 'da', $row['da'] );
     $e->setAttribute( 'mont', $row['mont'] );
     $e->setAttribute( 'yea', $row['yea'] );
     $e->setAttribute( 'comit', $row['comit'] );

     $r->appendChild( $e );

} print $ doc-&gt; saveXML();

&GT;

这是附带的脚本。

1 个答案:

答案 0 :(得分:0)

必须在php脚本中的每个变量周围加上utf8_encode($ variable)才能运行。

相关问题