Android的HttpClient的和UTF-8 [英] android httpclient and utf-8
问题描述
我试图连接到Web服务在我的querysting保存一些数据。坏的是,这个数据包含UTF-8 charcters,这使得一个问题。
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.
如果我简单地调用与HTTPGET我得到非法字符异常普通的字符串。于是,我用Google搜索,并尝试了一些UTF-8的法宝。
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());
现在我不会得到非法字符内,但它似乎惹UPP的utfurl完全因为我,而不是让目标主机不能为空,或参数设置。大概是因为他不承认的http://在搞砸了字符串的一部分。 有什么建议?
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?
问候
推荐答案
我想你想的URL连接codeD查询字符串。如果是这样,使用:
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");
其基本思想是,你只需要连接code中的查询字符串,而不是主机名或协议。
The basic idea is that you only want to encode the query string, not the host name or protocol.
这篇关于Android的HttpClient的和UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!