2

In JSON response of php webservice i get &#039 instead of apostrophe,How can I parse the JSON response with special characters in android?

Edited

My Http connection method code -

    InputStream is = null;
    String response = null,value = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet(url);    
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } 
    catch (UnknownHostException e) {
      e.printStackTrace();  
    }
    catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        String line = null;
        while ((line = reader.readLine()) != null) {
            value=line.trim();
        }
        is.close();
        response = URLEncoder.encode(value,"UTF-8");
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

When apply URLDecoder no effect on response and when try with URLEncoder in response some symbols and extra char are appeded like (%20d/).

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68

2 Answers2

1

Using the Html class we can do this:

 String example;
 if (Build.VERSION.SDK_INT >= 24) {
     example = Html.fromHtml(String, int) // for 24+ api
 } else {
     example = Html.fromHtml(String) // for older api
 }

Please see documentation https://developer.android.com/reference/android/text/Html.html

Arunkrishna
  • 416
  • 1
  • 4
  • 18
0

First it sounds like nothing to do with JSON and the charset for the InputStream, you get a segment of HTML text from the Web service, not a pure POJO.

Second the URLEncoder or URLEncoder does not help for &#XXX type escaping, they escape "'" as %27 and "," as %2C.

Last the fromHTML does the trick to parse HTML string, the API is

public static Spanned fromHtml(String source);

in the android.text.HTML object. The return value for the

HTML.fromHTML("&#039")

is "'".

Zephyr
  • 6,123
  • 34
  • 33