且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

德code字符串连接codeD在Android的UTF-8格式

更新时间:2023-11-27 20:25:40

您可以使用String构造与charset参数:

 尝试
{
    最终的String =新的String(nodevalue.getBytes(),UTF-8);
}
赶上(UnsupportedEncodingException E)
{
    Log.e(UTF-8,转换,E);
}
 

此外,因为您从XML文档获取数据,我认为它是连接codeD UTF-8,这可能是问题出在解析它。

您应该使用的InputStream / 的InputSource ,而不是的XMLReader 实现,因为它带有编码。所以,如果你是从一个HTTP响应获取这个数据,您既可以同时使用的InputStream 的InputSource

 尝试
{
    HttpEntity实体= response.getEntity();
    在= entity.getContent最终的InputStream();
    最后的SAXParser解析器= SAXParserFactory.newInstance()newSAXParser()。
    最后XmlHandler处理器=新XmlHandler();
    读者阅读=新InputStreamReader的(在UTF-8);
    InputSource的是=新的InputSource(读卡器);
    is.setEncoding(UTF-8);
    parser.parse(是,处理程序);
    // TODO:从你的处理程序获取数据
}
赶上(最终例外五)
{
    Log.e(ParseError,错误解析XML,E);
}
 

或只是的InputStream

 尝试
{
    HttpEntity实体= response.getEntity();
    在= entity.getContent最终的InputStream();
    最后的SAXParser解析器= SAXParserFactory.newInstance()newSAXParser()。
    最后XmlHandler处理器=新XmlHandler();
    parser.parse(在,处理程序);
    // TODO:从你的处理程序获取数据
}
赶上(最终例外五)
{
    Log.e(ParseError,错误解析XML,E);
}
 

更新1

下面是一个完整的请求和响应处理的例子:

 尝试
{
    最后DefaultHttpClient客户端=新DefaultHttpClient();
    最后HttpPost httppost =新HttpPost(http://example.location.com/myxml);
    最后的Htt presponse响应= client.execute(httppost);
    最终HttpEntity实体= response.getEntity();

    在= entity.getContent最终的InputStream();
    最后的SAXParser解析器= SAXParserFactory.newInstance()newSAXParser()。
    最后XmlHandler处理器=新XmlHandler();
    parser.parse(在,处理程序);
    // TODO:从你的处理程序获取数据
}
赶上(最终例外五)
{
    Log.e(ParseError,错误解析XML,E);
}
 

更新2

由于问题不是编码,但源XML被转义为HTML实体,***的解决办法是(除了修正PHP来不要逃避的反应),使用的 apache.commons.lang库的非常方便的静态StringEscapeUtils类

导入库,在你的XML处理程序的字符方法,你把下面后:

  @覆盖
公共无效字符(字符决赛[] CH,最终诠释开始,最终诠释长度)
    抛出的SAXException
{
    //这个变量将持有正确的转义值
    最后弦乐elementValue = StringEscapeUtils。
        unescapeHtml(新的String(CH,开始,长度).trim());
    [...]
}
 

更新3

在C中的问题,你最后$ C $与的nodeValue 变量的初始化。它应该是:

 字符串的nodeValue = StringEscapeUtils.unescapeHtml(
    新的String(CH,开始,长度).trim());
 

I have a string which comes via an xml , and it is text in German. The characters that are German specific are encoded via the UTF-8 format. Before display the string I need to decode it.

I have tried the following:

try {
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    new ByteArrayInputStream(nodevalue.getBytes()), "UTF8"));
    event.attributes.put("title", in.readLine());
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I have also tried this:

try {
    event.attributes.put("title", URLDecoder.decode(nodevalue, "UTF-8"));
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

None of them are working. How do I decode the German string

thank you in advance.

UDPDATE:

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    // TODO Auto-generated method stub
    super.characters(ch, start, length);
    if (nodename != null) {
        String nodevalue = String.copyValueOf(ch, 0, length);
        if (nodename.equals("startdat")) {
            if (event.attributes.get("eventid").equals("187")) {
            }
        }
        if (nodename.equals("startscreen")) {
            imageaddress = nodevalue;
        }
        else {
            if (nodename.equals("title")) {
                // try {
                // BufferedReader in = new BufferedReader(
                // new InputStreamReader(
                // new ByteArrayInputStream(nodevalue.getBytes()), "UTF8"));
                // event.attributes.put("title", in.readLine());
                // } catch (UnsupportedEncodingException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // } catch (IOException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // }
                // try {
                // event.attributes.put("title",
                // URLDecoder.decode(nodevalue, "UTF-8"));
                // } catch (UnsupportedEncodingException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // }
                event.attributes.put("title", StringEscapeUtils
                        .unescapeHtml(new String(ch, start, length).trim()));
            } else
                event.attributes.put(nodename, nodevalue);
        }
    }
}

You could use the String constructor with the charset parameter:

try
{
    final String s = new String(nodevalue.getBytes(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
    Log.e("utf8", "conversion", e);
}

Also, since you get the data from an xml document, and I assume it is encoded UTF-8, probably the problem is in parsing it.

You should use InputStream/InputSource instead of a XMLReader implementation, because it comes with the encoding. So if you're getting this data from a http response, you could either use both InputStream and InputSource

try
{
    HttpEntity entity = response.getEntity();
    final InputStream in = entity.getContent();
    final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final XmlHandler handler = new XmlHandler();
    Reader reader = new InputStreamReader(in, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    parser.parse(is, handler);
    //TODO: get the data from your handler
}
catch (final Exception e)
{
    Log.e("ParseError", "Error parsing xml", e);
}

or just the InputStream:

try
{
    HttpEntity entity = response.getEntity();
    final InputStream in = entity.getContent();
    final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final XmlHandler handler = new XmlHandler();
    parser.parse(in, handler);
    //TODO: get the data from your handler
}
catch (final Exception e)
{
    Log.e("ParseError", "Error parsing xml", e);
}

Update 1

Here is a sample of a complete request and response handling:

try
{
    final DefaultHttpClient client = new DefaultHttpClient();
    final HttpPost httppost = new HttpPost("http://example.location.com/myxml");
    final HttpResponse response = client.execute(httppost);
    final HttpEntity entity = response.getEntity();

    final InputStream in = entity.getContent();
    final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    final XmlHandler handler = new XmlHandler();
    parser.parse(in, handler);
    //TODO: get the data from your handler
}
catch (final Exception e)
{
    Log.e("ParseError", "Error parsing xml", e);
}

Update 2

As the problem is not the encoding but the source xml being escaped to html entities, the best solution is (besides correcting the php to do not escape the response), to use the apache.commons.lang library's very handy static StringEscapeUtils class.

After importing the library, in your xml handler's characters method you put the following:

@Override
public void characters(final char[] ch, final int start, final int length) 
    throws SAXException
{
    // This variable will hold the correct unescaped value
    final String elementValue = StringEscapeUtils.
        unescapeHtml(new String(ch, start, length).trim());
    [...]
}

Update 3

In your last code the problem is with the initialization of the nodevalue variable. It should be:

String nodevalue = StringEscapeUtils.unescapeHtml(
    new String(ch, start, length).trim());