Android 中调用 REST Web 服务时的 UTF8 编码

发布于 2024-10-10 13:57:55 字数 1128 浏览 7 评论 0原文

我正在调用返回 XML 的剩余 WS。有些元素的字符串包括特殊字符,如 áãç 等...... 当我通过浏览器获取信息时,所有信息都会正确显示,但是当从 Android 调用它时,我没有获得正确的特殊字符。

注意“解码”和“编码”变量:

当我使用 URLDecoder.decode(结果, "UTF-8") 结果保持不变

当我使用时 URLEncoder.encode(result, "UTF-8") 结果更改为预期的结果(充满 % 的符号以及代表符号和特殊字符的数字)。

这是调用网络服务的方法:

public void updateDatabaseFromWebservice(){

    // get data from webservice
    Log.i(TAG, "Obtaining categories from webservice");

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(ConnectionProperties.CATEGORIES_URI);

    ResponseHandler<String> handler = new BasicResponseHandler();

    String result = "";
    String decoded;
    String encoded;
    try {                   
        result = client.execute(request, handler);  

        decoded = URLDecoder.decode(result, "UTF-8");
        encoded = URLEncoder.encode(result, "UTF-8");
        String c = "AS";

    } catch (Exception e) {  
        Log.e(TAG, "An error occurred while obtaining categories", e);
    }

    client.getConnectionManager().shutdown();
}

任何帮助将不胜感激

I'm invoking a rest WS that returns XML. Some elements have strings include special characters like áãç etc...
When I get the information via browser all of it is shown properly but when invoking it from Android I don't get the proper special characters.

Notice the 'decoded' and 'encoded' variables:

when I use
URLDecoder.decode(result, "UTF-8")
The result stays the same

when I use
URLEncoder.encode(result, "UTF-8") The result changes to what it would be expected (full of %'s symbols and numeric representing symbols and special characters).

Here's the method to call the webservice:

public void updateDatabaseFromWebservice(){

    // get data from webservice
    Log.i(TAG, "Obtaining categories from webservice");

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(ConnectionProperties.CATEGORIES_URI);

    ResponseHandler<String> handler = new BasicResponseHandler();

    String result = "";
    String decoded;
    String encoded;
    try {                   
        result = client.execute(request, handler);  

        decoded = URLDecoder.decode(result, "UTF-8");
        encoded = URLEncoder.encode(result, "UTF-8");
        String c = "AS";

    } catch (Exception e) {  
        Log.e(TAG, "An error occurred while obtaining categories", e);
    }

    client.getConnectionManager().shutdown();
}

Any help would be appreciated

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

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

发布评论

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

评论(2

橘香 2024-10-17 13:57:55

使用它来获取 xml 字符串,假设服务器以 UTF-8 编码数据:

HttpResponse response = client.execute(request);
... // probably some other code to check for HTTP response status code
HttpEntity responseEntity = response.getEntity();
String xml = EntityUtils.toString(responseEntity, HTTP.UTF_8);

Use this to get xml string, assuming the server encodes data in UTF-8:

HttpResponse response = client.execute(request);
... // probably some other code to check for HTTP response status code
HttpEntity responseEntity = response.getEntity();
String xml = EntityUtils.toString(responseEntity, HTTP.UTF_8);
听你说爱我 2024-10-17 13:57:55

呃。 URLDecoder 和编码器用于编码和解码 URL,而不是 XML 内容。它用于您在发出请求时使用的 URL。所以代码只是......错误的。

但更大的问题是您使用的是字符串,而内容实际上是需要解析的 XML。为了让解析器正确解码 UTF-8(以及处理实体等),最好从请求中获取 byte[],并将其传递给解析器;尽管要求 http 客户端进行解码可能可以正常工作(假设服务正确指示所使用的编码;并非所有都这样做 - 但即使没有,XML 解析器也可以从 xml 声明中找出它)。

因此:删除 URLDecoder/URLEncoder 内容,解析 XML,并从 XML 中提取所需的数据。

Uh. URLDecoder and encoder are for encoding and decoding URLs, not XML content. It is used for URL you use when making requests. So code is just... wrong.

But even bigger issue is that you are taking a String, whereas content is really XML which needs to be parsed. And for parser to do proper decoding of UTF-8 (and handling of entities etc), you would be better of getting a byte[] from request, passing that to parser; although asking http client to do decoding may work ok (assuming service correctly indicates encoding used; not all do -- but even if not, XML parsers can figure it out from xml declaration).

So: remove URLDecoder/URLEncoder stuff, parser XML, and extract data you want from XML.

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