android httpclient 和 utf-8

发布于 2024-10-17 09:44:30 字数 733 浏览 3 评论 0原文

我正在尝试连接到一个网络服务,我的查询中保存了一些数据。不好的是,这些数据包含utf-8字符,这会出现问题。

如果我只是使用普通字符串调用 HttpGet,则会出现“非法字符”异常。所以我用谷歌搜索并尝试了一些 utf-8 魔法。

            HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
        String utfurl = URLEncoder.encode(url, "utf-8");
        HttpGet httpGet = new HttpGet(utfurl);
        HttpResponse response = httpclient.execute(httpGet);
        content = response.getEntity().getContent();
            } catch (Exception e) {
                Log.d(TAG, "getInputStream: " +e.getMessage());

现在我不会得到非法字符,但它似乎完全弄乱了 utfurl,因为我得到了“目标主机不能为空,或在参数中设置”。可能是因为他无法识别混乱字符串中的“http://”部分。 有什么建议吗?

问候

I'm trying to connect to a webservice where my querysting holds some data. The bad thing is that this data contains utf-8 charcters, which renders a problem.

If I simply call HttpGet with the ordinary string I get the "illegal character" exception. So I googled and tried some utf-8 magic.

            HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
        String utfurl = URLEncoder.encode(url, "utf-8");
        HttpGet httpGet = new HttpGet(utfurl);
        HttpResponse response = httpclient.execute(httpGet);
        content = response.getEntity().getContent();
            } catch (Exception e) {
                Log.d(TAG, "getInputStream: " +e.getMessage());

Now I won't get the illegal charcter, but it seem to mess upp the utfurl completely since I instead get the "target host must not be null, or set in parameters". Probably because he doesnt recognize the "http://" part in a messed up string.
Any advice?

Regards

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

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

发布评论

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

评论(1

谁许谁一生繁华 2024-10-24 09:44:30

我认为您需要一个 URL 编码的查询字符串。如果是这样,请使用:

String query = "?param=value";
String host = "http://my.host.name.com/";
String encodedUrl = host + UrlEncoder.encode(query,"utf-8");

基本思想是您只想对查询字符串进行编码,而不是主机名或协议。

I think you want a URL encoded query string. If so, use:

String query = "?param=value";
String host = "http://my.host.name.com/";
String encodedUrl = host + UrlEncoder.encode(query,"utf-8");

The basic idea is that you only want to encode the query string, not the host name or protocol.

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