首页 > 代码库 > volley接受数据时中文会出现乱码

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

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 one rrorResponse(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接受数据时中文会出现乱码