javaandroidgoogle-elevation-api

Using Maps Elevation API


In my app, when I try to get an elevation with the "Maps Elevation API", I get an answer which looks like a Chinese answer. My request is : My request

The result returned by the API is queried with the below code :

            try {
            url = new URL(arg0[0]);
            urlConnection = (HttpURLConnection)url.openConnection();
            try {
                urlConnection.setRequestMethod("POST");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                os=urlConnection.getOutputStream();
                bw=new BufferedWriter(new OutputStreamWriter(os,"UTF_16"));
                bw.flush();
                bw.close();
                is = urlConnection.getInputStream();
                br = new BufferedReader(new InputStreamReader(is, "UTF_16"));
                while ((ligneRéponse = br.readLine()) != null) {
                    if(ligneRéponse.length()!=0) {e
                        if (ligneRéponse.contains("<elevation>")) {
                            if (ligneRéponse.contains("<elevation>")) {
                                altitude = Double.parseDouble(ligneRéponse.substring(ligneRéponse.indexOf("<elevation>") + 11, ligneRéponse.indexOf("</elevation>")));
                            } else {
                                altitude = -10000;
                            }
                        }
                    }
                }
                is.close();
            } finally {
                urlConnection.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return(altitude);

Until recently, this code was working perfectly. Now, what I get in my ligneRéponse variable looks like this : 㰿硭氠癥牳楯渽∱⸰∠敮捯摩湧㴢啔䘭㠢㼾਼䕬敶慴楯湒敳灯湳放ਠ㱳瑡瑵猾佋㰯獴慴畳㸊‼牥獵汴㸊†㱬潣慴楯渾ਠ†㱬慴㸴㔮㠱ㄲ㠰㜼⽬慴㸊†‼汮朾㌮〱㠴㘹㔼⽬湧㸊†㰯汯捡瑩潮㸊†㱥汥癡瑩潮㸸〳⸶㌶㜱㠸㰯敬敶慴楯渾ਠ‼牥獯汵瑩潮㸱㤮〸㜹〴〼⽲敳潬畴楯渾ਠ㰯牥獵汴㸊㰯䕬敶慴楯湒敳灯湳放�

If I launch my request directly from my web browser, the result is correctly written in english, as it used to be...

Does anyone know how I can get an understandable answer in my app?


Solution

  • The answer comes in UTF-8, but you are trying to interpret it as UTF-16.