0

English text and numbers are displayed in result string correctly, but other letters i wish to display are not. Instead of them there are a sequences of symbols like &-#-1080; ... I tried to set UTF-8 on my InputStreamReader, but it has no effect. Appreciate any help. Here's the code:

public class HttpClient {
private static String BASE_URL = "here url";
public String getData(String number) {
    HttpURLConnection con = null ;
    InputStream is = null;
    try {
        con = (HttpURLConnection) ( new URL(BASE_URL + number)).openConnection();
        con.setRequestMethod("GET");
        con.setDoInput(true);
        con.setDoOutput(true);
        con.connect();
        // Let's read the response
        StringBuffer buffer = new StringBuffer();
        is = con.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        String line = null;
        while (  (line = br.readLine()) != null )
            buffer.append(line + "\r\n");

        is.close();
        con.disconnect();
        return buffer.toString();
    }
    catch(Throwable t) {
        t.printStackTrace();
    }
    finally {
        try { is.close(); } catch(Throwable t) {}
            try { con.disconnect(); } catch(Throwable t) {}
        }
        return null;
    }
}

------------
JSONObject jb = new JSONObject(data);
JSONArray jr = jb.getJSONArray("items");
JSONObject c = jr.getJSONObject(0);
String id = c.getString(TAG_ID);
String images = c.getString(TAG_IMAGES);
String title = new String(c.getString(TAG_TITLE).getBytes("ISO-8859-1"), "UTF-     8");//tried this - didn't help
String text = c.getString(TAG_TEXT);`
Max Makeichik
  • 227
  • 4
  • 18

2 Answers2

0

Are you sure that the response is UTF-8?

Try hitting the URL in a browser and take a look at the HTTP response headers, I'm guessing it's not UTF-8.

There's many libraries out there to help with web service requests. One of the more low-level libraries is http-client. I suggest you use this (or a higher level abstraction) rather than using HttpURLConnection directly.

lance-java
  • 25,497
  • 4
  • 59
  • 101
-1

If you create json from php web service use this code

echo utf8_encode(json_encode($result));