Bing 地图 REST 服务返回的法语字符不正确。有没有办法要求他们对响应进行编码或以某种方式解码?

发布于 2024-12-09 08:48:18 字数 1950 浏览 0 评论 0 原文

在我编写的Android应用程序中,有一部分允许用户输入行程的起点和终点,并返回路线行程。我为此使用 Bing 地图 REST 服务。我希望返回的指示是法语的。

请求示例:请求。这在 Chrome 浏览器上最明显,Safari 和 Firefox 可以解决这个问题。您可以看到说明书上有很多不应该出现的奇怪字符。我尝试在设备上进行解码,方法是:

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

这不起作用(响应保持不变),这是有道理的我想既然它已经成为特殊字符了。我无法使用 Windows-1252 进行解码,因为 Android 似乎不支持它。

我被退回的示例:Léger Encombrement。它应该是什么:Léger Encombrement

它在 iPhone 上也能完美运行,但在 Android 上则不行。关于如何解决这个问题有什么建议吗?

我在连接类中的代码是:

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 JSONObject(str);
            Log.d("connection", "JSON RESPONSE IS " + obj);
            return obj;

        } else {
            return null;
        }

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

}

我需要在连接类中添加一些内容吗?

更新:

我添加了 JSON 解析代码,格式为“ISO_8859_1”,如以下链接所示: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ 但我仍然得到相同的结果结果 ...

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 JSONObject(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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-12-16 08:48:18

这是 JSON。您不需要使用 URLDecoder。错误发生在这之前,可能是在您为 JSON 解析器创建字符串时。 JSON 始终采用 UTF-8(或 16,很少)

您可以发布用于读取服务器响应的代码吗?

编辑

如果在内容中找不到 ISO_8859_1,则 EntityUtils 使用 ISO_8859_1 作为默认字符集。只需更改

final String str = EntityUtils.toString(resEntity);

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文