Volley JsonObjectRequest 获取json字符串乱码
来源:互联网 发布:网络接口都有哪些 编辑:程序博客网 时间:2024/04/26 05:34
在使用Volley过程遇到一个问题,使用JsonObjectRequest访问http://www.weather.com.cn/adat/cityinfo/101200101.html 得到的 数据是乱码,查看源码发现在JsonObjectRequest类中代码如下
@Overrideprotected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {try {<span style="color:#ff6666;">String jsonString = new String(response.data,HttpHeaderParser.parseCharset(response.headers));</span>return Response.success(new JSONObject(jsonString),HttpHeaderParser.parseCacheHeaders(response));} catch (UnsupportedEncodingException e) {return Response.error(new ParseError(e));} catch (JSONException je) {return Response.error(new ParseError(je));}}这行代码是设置编码的方法,参数是响应的头信息 进去看一下
/** * Returns the charset specified in the Content-Type of this header, * or the HTTP default (ISO-8859-1) if none can be found. */ public static String parseCharset(Map<String, String> headers) { String contentType = headers.get(HTTP.CONTENT_TYPE); if (contentType != null) { String[] params = contentType.split(";"); for (int i = 1; i < params.length; i++) { String[] pair = params[i].trim().split("="); if (pair.length == 2) { if (pair[0].equals("charset")) { return pair[1]; } } } } return HTTP.DEFAULT_CONTENT_CHARSET; }
从注释就可以看出,这段代码会从头信息获取编码,如果没有的话就会默认设置成ISO-8859-1,此时我们的解决方法有两个
1.修改服务端编码ISO-8859-1,此方法此时不使用,因为服务端代码并不是我们编写的,
2.修改本地编码,自己定义类继承JsonObjectRequest,自由设置编码
public class MyJsonObjectRequest extends JsonObjectRequest {public MyJsonObjectRequest(int method, String url, JSONObject jsonRequest,Listener<JSONObject> listener, ErrorListener errorListener) {super(method, url, jsonRequest, listener, errorListener);}public MyJsonObjectRequest(String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener) { this(jsonRequest == null ? Method.GET : Method.POST, url, jsonRequest, listener, errorListener); }@Overrideprotected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {try {String jsonString = new String(response.data,"utf-8");return Response.success(new JSONObject(jsonString),HttpHeaderParser.parseCacheHeaders(response));} catch (UnsupportedEncodingException e) {return Response.error(new ParseError(e));} catch (JSONException je) {return Response.error(new ParseError(je));}}}
使用我们定义的MyJsonObjectRequest就可以轻松实现转码
0 0
- Volley JsonObjectRequest 获取json字符串乱码
- volley获取json数据乱码,解决方法
- Volley获取json数据乱码问题
- volley JsonObjectRequest 提交参数
- Volley使用之JsonObjectRequest
- volley获取json数据及中文乱码问题的解决
- volley获取json数据及中文乱码问题的解决
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
- volley字符串乱码问题
- 初识Volley:JsonObjectRequest、StringRequest、RequestQueue
- Volley中JsonObjectRequest的使用
- 解决Volley框架下JsonObjectRequest以POST提交数据,服务端无法获取数据的问题。
- 解决Volley框架下JsonObjectRequest以POST提交数据,服务端无法获取数据的问题。
- Volley获取Json数据异常
- Volley StringRequest和JSONObjectRequest使用几个细节
- Android学习记录之Volley框架JSONObjectRequest
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley使用JsonObjectRequest发送Post请求失败
- Android中的AndroidAnnotations
- c#自定义控件
- 扣丁学堂——AlarmManager
- 关于setsid() 函数的说明
- 山东省第六届蓝桥杯 ///标题:奖券数目//c/c++组
- Volley JsonObjectRequest 获取json字符串乱码
- 解析检测先锋之气体传感器的技术应用
- Java 开发必会的 Linux 命令
- 为了取消老毛桃的主页设置而发的博客,没有用,不要点开
- android开发仿iphone开关按钮动态效果
- Git Reference---Sharing and Updating Projects
- CocoaAsyncSocket使用详解
- 更改DOS窗口用户名C:\Users\XXX>
- java+poi解析excel文件