开发者

Bing Maps REST services returns incorrect characters for French. Is there a way to request them to encode their response or decode it somehow?

开发者 https://www.devze.com 2023-04-12 08:02 出处:网络
In the Android application I have written, there is a portion which allows the user to enter the start and end location of their trip, and a route itinerary is returned. I am using Bing Maps REST serv

In the Android application I have written, there is a portion which allows the user to enter the start and end location of their trip, and a route itinerary is returned. I am using Bing Maps REST services for this. I want the directions returned to be in French.

A sample request: request. This is best seen on a Chrome browser, Safari and Firefox take care of this. You can see that the directions have lots of strange characters where they are not supposed to be. I have tried decoding on the device, by doing:

URLDecoder.decode(obj.optString("text"), HTTP.ISO_8859_1)

which does not work (the response stays the same), which makes sense I think since it has already become the special characters. I cannot use Windows-1252 to decode because Android does not seem to support that.

An example of what I am being sent back: Léger Encombrement. What it should be: Léger Encombrement.

It works perfectly on an iPhone as well, but not on Android. Any suggestions on how I can solve this?

My code in the connection class is:

public static JSONObject getJSONResult(final String url) {

    try {
        final HttpClient client = new DefaultHttpClient();
        final HttpGet get = new HttpGet(url);
        final HttpResponse responsePost = client.execute(get);
        final HttpEntity resEntity = responsePost.getEntity();

        boolean DEBUG = true;
        if (DEBUG) {
            Log.d("", "[JSON-ENV] url:  " + url);
        }

        final String str = EntityUtils.toString(resEntity);
        Log.d("connection", "response str: " + str);

        if (resEntity != null) {
            final JSONObject obj = new JSONO开发者_如何学Cbject(str);
            Log.d("connection", "JSON RESPONSE IS " + obj);
            return obj;

        } else {
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

Is there something I need to add into my connection class?

UPDATE:

I added the JSON parsing code to format as "ISO_8859_1" as seen at this link: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ but I still get the same results ...


This is JSON. You don't need to use URLDecoder. The error is before that, probably when you create the String for the JSON Parser. JSON is always in UTF-8 (or 16, rarely)

Can you post the code for reading the server response?

edit

EntityUtils uses ISO_8859_1 as a default Charset if it does not find one in the content. Simply change

final String str = EntityUtils.toString(resEntity);

to

final String str = EntityUtils.toString(resEntity, HTTP.UTF_8);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号