2011-04-29 66 views
11

我有一個字符串,它通過一個XML來,它是德文文本。德國特定的字符是通過UTF-8格式編碼的。在顯示字符串之前,我需要解碼它。解碼字符串編碼utf-8格式在android

我曾嘗試以下:

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(); 
} 

他們都不是工作。如何解碼德文字符串

謝謝您提前。

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); 
     } 
    } 
} 
+0

我無法在需要時找到此問題解答。因此,我現在已經複製了它,我希望這將快速彈出下一次 – Jayy 2013-04-30 13:51:27

回答

20

你可以使用String構造與charset參數:

​​

而且,因爲你從XML文檔獲取數據,我認爲它是編碼的UTF-8,可能是解析它的問題。

您應該使用InputStream/InputSource而不是XMLReader實現,因爲它帶有編碼。所以,如果你是從一個HTTP響應得到這個數據,您既可以同時使用InputStreamInputSource

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); 
} 

或只是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); 
} 

更新1

這裏一個完整的請求和響應處理示例:

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); 
} 

更新2

由於問題不是編碼,但源XML被轉義爲HTML實體,最好的辦法是(除了修正PHP做不逃避響應),使用apache.commons.lang library「非常方便static StringEscapeUtils class

導入庫,在你的XML處理程序的characters方法後你把以下內容:

@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()); 
    [...] 
} 

更新3

在您的最後一個碼的問題是與nodevalue變量的初始化。它應該是:

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

我在哪裏通過URL在這裏? – user590849 2011-04-29 09:10:38

+0

你的意思是從哪裏獲得XML數據響應的網址? – rekaszeru 2011-04-29 09:20:17

+0

是的。你使用的響應對象是什麼。它是HttpResponse對象嗎? – user590849 2011-04-29 09:21:55