-1

Made an app to translate different words to different Language Using Yandex converter getting proper results on Browser
converting Kiss
RESULTS as JSON object is {"code":200,"lang":"en-hi","text":["चुम्बन"]} //proper
but while getting result on app RESULT {"code":200,"lang":"en-hi","text":["à¤à¥à¤®à¥à¤¬à¤¨"]}

 JSONParser jParser = new JSONParser();
  // get json string from url
 JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

geJSONFromUrl function

public JSONObject getJSONFromUrl(String urlSource) {
    //make HTTP request
    try {
        URL url = new URL(urlSource);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);
        inputStream = new BufferedInputStream(urlConnection.getInputStream());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Read JSON data from inputStream
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        inputStream.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e(TAG, "Error parsing data " + e.toString());
    }

    return jObj;// return JSON String
}

}

Is there any way i can get proper results?
Please Help
Regards

phpdroid
  • 1,642
  • 1
  • 18
  • 36
  • Maybe you are working with a wrong text encoding? What does the function getJSONFromUrl look like? – Chris623 Aug 21 '16 at 22:20
  • added in the question – phpdroid Aug 22 '16 at 04:54
  • Would you please include the server side code. I think it's about encoding. In PHP (for example) you should use something like this [link](http://stackoverflow.com/questions/16498286/why-does-the-php-json-encode-function-convert-utf-8-strings-to-hexadecimal-entit). – Rouhollah Mazarei Aug 22 '16 at 05:25
  • @R.Mazarei no php code required getting result from this link https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20140720T191145Z.05605441c6ee16dc.eaaf6c6c8690cb5fb094cea2bfec4f787af6170c&lang=en-hi&text=Kiss – phpdroid Aug 22 '16 at 07:03
  • used in Code as `https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20140720T191145Z.05605441c6ee16dc.eaaf6c6c8690cb5fb094cea2bfec4f787af6170c&lang=en-hi&text="+ TextUtils.htmlEncode(MainActivity.toTranslate)` – phpdroid Aug 22 '16 at 07:04
  • @Chris623 you were right changed to utf-8 and worked – phpdroid Aug 23 '16 at 00:25

1 Answers1

0

changed

 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);

to

 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
phpdroid
  • 1,642
  • 1
  • 18
  • 36