elw*_*wis 1 android http-get httpclient utf-8
我正在尝试连接到我的查询包含一些数据的Web服务.坏的是这个数据包含utf-8字符,这会产生问题.
如果我只是用普通字符串调用HttpGet,我会得到"非法字符"异常.所以我用Google搜索并尝试了一些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());
Run Code Online (Sandbox Code Playgroud)
现在我不会得到非法的字符,但它似乎完全搞乱了utfurl,因为我反而得到"目标主机不能为空,或设置参数".可能是因为他不认识混乱的字符串中的"http://"部分.有什么建议?
问候
我想你想要一个URL编码的查询字符串.如果是这样,请使用:
String query = "?param=value";
String host = "http://my.host.name.com/";
String encodedUrl = host + UrlEncoder.encode(query,"utf-8");
Run Code Online (Sandbox Code Playgroud)
基本思想是您只想编码查询字符串,而不是主机名或协议.