android 阿拉伯语接收不到广播,Android不接受阿拉伯语xml并崩溃

我刚开始使用android。

问题在于,当我使用英语时,XML解析器不处理阿拉伯语言,效果很好。

正如你所看到的,我在XML文件中使用utf-8编码,如果我处理阿拉伯语言,应用程序将崩溃。

我的xml文件:

xml prarser代码:

public class XMLParser {

// constructor

public XMLParser() {

}

/**

* Getting XML from URL making HTTP request

* @param url string

* */

public String getXmlFromUrl(String url) {

String xml = null;

try {

// defaultHttpClient

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();

xml = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// return XML

return xml;

}

/**

* Getting XML DOM element

* @param XML string

* */

public Document getDomElement(String xml){

Document doc = null;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource();

is.setCharacterStream(new StringReader(xml));

doc = db.parse(is);

} catch (ParserConfigurationException e) {

Log.e("Error: ", e.getMessage());

return null;

} catch (SAXException e) {

Log.e("Error: ", e.getMessage());

return null;

} catch (IOException e) {

Log.e("Error: ", e.getMessage());

return null;

}

return doc;

}

/** Getting node value

* @param elem element

*/

public final String getElementValue( Node elem ) {

Node child;

if( elem != null){

if (elem.hasChildNodes()){

for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){

if( child.getNodeType() == Node.TEXT_NODE ){

return child.getNodeValue();

}

}

}

}

return "";

}

/**

* Getting node value

* @param Element node

* @param key string

* */

public String getValue(Element item, String str) {

NodeList n = item.getElementsByTagName(str);

return this.getElementValue(n.item(0));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值