I
I
ivan8m82016-12-17 16:38:14
Java
ivan8m8, 2016-12-17 16:38:14

Why does the same code execute differently on the emulator and on a real Android device (encoding)?

Good afternoon.
For a very long time I wrote an application for Android and tested it on an emulator.
When I decided to test it on a real device, the application crashed.
After some time, we managed to find out that the problem is that it is not possible to get an array using JSON.
In the log when running on the emulator, I see that the answer comes as follows:

{"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"Vernadsky Avenue, ...

In the logs when running on a smartphone:
{"response":{"GeoObjectCollection":{"metaDataProperty":{"GeocoderResponseMetaData":{"request":"????? ??????, ????? ...

In principle, I see that the message is lost after it gets to their server ( https://geocode-maps.yandex.ru).
Because in the same logs you can see that they receive a message:
...format=json&geocode=Prospect%20Vernadsky, %20Moscow

But now the answer comes from them, as I wrote above with question marks, instead of Vernadsky Avenue.
What can you advise? I already tried to encode in several encodings, the question marks in the answer also remain.
String preUrl = "https://geocode-maps.yandex.ru/1.x/?format=json&geocode="
                            + myObjects.get(position).getField.toString;
                    preUrl = preUrl.replace(" ", "%20");
                    URL url = new URL(preUrl);
                    Log.d("KSI++", url.toString());
                    URLConnection urlConnection = url.openConnection();
                    InputStream inputStream = urlConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder result = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        result.append(line);
                        Log.d("KSI18", result.toString());
                    }
                    try {
                        JSONObject jsonObject = new JSONObject(result.toString());
                        JSONArray jsonArray = jsonObject.getJSONObject("response").getJSONObject("GeoObjectCollection").getJSONArray("featureMember");
                        location = jsonArray.getJSONObject(0).getJSONObject("GeoObject").getJSONObject("Point").getString("pos");
                        Log.d("KSI++++", location + " EHRERERE!");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivan8m8, 2016-12-17
@ivan8m8

I decided. URLEncoder.

String preUrl = "https://geocode-maps.yandex.ru/1.x/?format=json&geocode="
                            + URLEncoder.encode(myObjects.get(position).getField.toString, "UTF-8");

+ stackoverflow.com/questions/10786042/java-url-enco...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question