android httpclient 和 utf-8
我正在尝试连接到一个网络服务,我的查询中保存了一些数据。不好的是,这些数据包含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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要一个 URL 编码的查询字符串。如果是这样,请使用:
基本思想是您只想对查询字符串进行编码,而不是主机名或协议。
I think you want a URL encoded query string. If so, use:
The basic idea is that you only want to encode the query string, not the host name or protocol.