码迷,mamicode.com
首页 > 其他好文 > 详细

volley接受数据时中文会出现乱码

时间:2016-09-21 22:57:18      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

private void doVolley(){
RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity());

StringRequest mStringRequest = new StringRequest(
"http://www.baidu.com",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String s = response;
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.loadDataWithBaseURL(null,s,"text/html","utf-8",null);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mWebView.loadDataWithBaseURL(null,"失败","text/html","utf-8",null);
}
}
){
//这是重写的方法,源代码默认是外国人用的ISO-8859-1,现在改成utf-8格式

@Override
protected Response<String> parseNetworkResponse(
NetworkResponse response) {

try {//jsonObject要和前面的类型一致,此处都是String
String jsonObject = new String(
new String(response.data, "UTF-8"));
return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (Exception je) {
return Response.error(new ParseError(je));
}
}
};
mRequestQueue.add(mStringRequest);

}

volley接受数据时中文会出现乱码

标签:

原文地址:http://www.cnblogs.com/lyf2458857555/p/5894453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!