1
Possible Duplicate:
Parsing an UTF-8 Encodded XML file畸形阿拉伯語中的HttpResponse
我解析其中包含一些阿拉伯字符一切工作正常,除了阿拉伯字符不顯示UTF-8編碼的XML文件,一些奇怪的字符顯示如下:
ÙرÙÙ
這裏是鏈接到XML 「http://212.12.165.44:7201/UniNews121.xml」 文件正在解析
下面是代碼
public String getXmlFromUrl(String url) {
try {
return new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
//String xml = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8");
HttpGet httpPost = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = new String(EntityUtils.toString(httpEntity).getBytes(),"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
//just to remove the BOM Element
xml=xml.substring(3);
//Here am printing the xml and the arabic chars are malformed
Log.i("DEMO", xml);
return xml;
}
}.execute(url).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
}
請注意,沒有錯誤發生,一切正常,只是阿拉伯文字符格式不正確。
我很感激你的幫助,但請具體說明在你的答案
感謝的人,它的工作在這個階段,但現在我面對的另一個問題 –
09-18 13:18:21.022:E /錯誤:(3750):意外令牌(位置:TEXT XML版本=「1.0 ... @ 2:1 [email protected]) 當我爲此 StringReader的xmlString =新StringReader(XML); \t \t \t is.setCharacterStream(的xmlString); \t \t \t is.setEncoding(「UTF-8」); \t \t \t doc = db.parse(is); –
我沒有看到「is」在這裏,也不知道你爲什麼要在xml字符串上使用StringReader。我建議你發佈一個新的問題,這個 – njzk2