Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use the String constructor with the charset parameter: </p> <pre><code>try { final String s = new String(nodevalue.getBytes(), "UTF-8"); } catch (UnsupportedEncodingException e) { Log.e("utf8", "conversion", e); } </code></pre> <p>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. </p> <p>You should use <code>InputStream</code>/<code>InputSource</code> instead of a <code>XMLReader</code> implementation, because it comes with the encoding. So if you're getting this data from a http response, you could either use both <code>InputStream</code> and <code>InputSource</code></p> <pre><code>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); } </code></pre> <p>or just the <code>InputStream</code>:</p> <pre><code>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); } </code></pre> <p><strong>Update 1</strong></p> <p>Here is a sample of a complete request and response handling:</p> <pre><code>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); } </code></pre> <p><strong>Update 2</strong></p> <p>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 <a href="http://commons.apache.org/lang/download_lang.cgi" rel="noreferrer">apache.commons.lang library</a>'s very handy <code>static StringEscapeUtils class</code>.</p> <p>After importing the library, in your xml handler's <code>characters</code> method you put the following: </p> <pre><code>@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()); [...] } </code></pre> <p><strong>Update 3</strong></p> <p>In your last code the problem is with the initialization of the <code>nodevalue</code> variable. It should be:</p> <pre><code>String nodevalue = StringEscapeUtils.unescapeHtml( new String(ch, start, length).trim()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload