Android 中用HttpClient进行网络数据加载时,得到String乱码的问题
2016-04-10 12:22
573 查看
当我们用HttpClient进行String加载或者想要得到JSON字符串时,有时会出现得到的字符串乱码,代码如下:
public class HttpUtil {
public static String getJson(String path){
ByteArrayOutputStream baos=null;
String str=null;
try {
HttpGet get=new HttpGet(path);
HttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
InputStream is=response.getEntity().getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
while(reader.readLine()!=null){
baos.write(buffer, 0, buffer.length);
}
str=new String(buffer, "utf-8");
is.close();
reader.close();
baos.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
在上述代码中,如果将得到的Entity对象转换成流进行操作,很容易乱码,就算指定编码格式也会出现乱码情况,这种情况是有什么原因造成,目前我也不太清楚,所以如果出现乱码,用EntityUtils.toString(httpResponse.getEntity())方法,直接转成字符串,就不会出现乱码了!
try {
HttpGet get=new HttpGet(path);
HttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
str=EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
public class HttpUtil {
public static String getJson(String path){
ByteArrayOutputStream baos=null;
String str=null;
try {
HttpGet get=new HttpGet(path);
HttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
InputStream is=response.getEntity().getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
while(reader.readLine()!=null){
baos.write(buffer, 0, buffer.length);
}
str=new String(buffer, "utf-8");
is.close();
reader.close();
baos.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
在上述代码中,如果将得到的Entity对象转换成流进行操作,很容易乱码,就算指定编码格式也会出现乱码情况,这种情况是有什么原因造成,目前我也不太清楚,所以如果出现乱码,用EntityUtils.toString(httpResponse.getEntity())方法,直接转成字符串,就不会出现乱码了!
try {
HttpGet get=new HttpGet(path);
HttpClient client=new DefaultHttpClient();
HttpResponse response=client.execute(get);
if(response.getStatusLine().getStatusCode()==200){
str=EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
相关文章推荐
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析
- C#操作 JSON方法汇总
- JavaScript中字符串(string)转json的2种方法
- js Object2String方便查看js对象内容