重写parseNetworkResponse方法,对其编码进行转换。
//获取天气数据 public void getWeatherInfo(){ StringRequest stringRequest = new StringRequest(Nan_changURL, new Response.Listener<String>() { @Override public void onResponse(String response) { //content.setText(response); WeatherBean bean = getWeatherFromJson(response);//info为正确编码解析后的json数据,在bean中就可以获取第一级的若干参数 final ChildWeatherBean childWeatherBean = bean.data;//这是第二级的javabean,可获取当下的温度以及一些基本参数 Weather.ChildWeatherBean.GrandChildWeatherBean grandChildWeahterBean = childWeatherBean.yesterday; //yesterday中的内容算是第三级的内容了,下面这句话也是第三级的内容,只不过这是一个List集合,这里就可以直接获取到了。 final ArrayList<Weather.ChildWeatherBean.GrandChildsWeatherBean> grandChildsWeatherBeans = childWeatherBean.forecast; content.setText(grandChildWeahterBean.fx); pd.dismiss(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ //volley解决中文乱码问题,真的很有用哦!!!要写个博客备注一下 @Override protected Response<String> parseNetworkResponse(NetworkResponse response){ try { String jsonString = new String(response.data, "UTF-8"); return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (Exception je) { return Response.error(new ParseError(je)); } } }; mQueue.add(stringRequest); }
gson解析json数据问题:gson将json转换文实体类、有几层我们就得建几个实体类;
/** * */ package com.zhijianchanghang.model; import java.util.ArrayList; /** * @Description: TODO(用一句话描述该文件做什么) * @author daipengcheng * @date 2015-12-20 下午4:37:20 * { "desc": "OK", "status": 1000, "data": { "wendu": "7", "ganmao": "昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。", "forecast": [ { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 12℃", "type": "多云", "low": "低温 6℃", "date": "21日星期一" }, { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 12℃", "type": "小到中雨", "low": "低温 9℃", "date": "22日星期二" }, { "fengxiang": "无持续风向", "fengli": "微风级", "high": "高温 12℃", "type": "中雨", "low": "低温 9℃", "date": "23日星期三" }, { "fengxiang": "北风", "fengli": "3-4级", "high": "高温 11℃", "type": "小雨", "low": "低温 7℃", "date": "24日星期四" }, { "fengxiang": "北风", "fengli": "3-4级", "high": "高温 9℃", "type": "多云", "low": "低温 4℃", "date": "25日星期五" } ], "yesterday": { "fl": "微风", "fx": "无持续风向", "high": "高温 8℃", "type": "小雨", "low": "低温 4℃", "date": "20日星期日" }, "aqi": "106", "city": "南昌" } } * @version V1.0 */ public class Weather { public class WeatherBean { public ChildWeatherBean data; public int status; public String desc; } public class ChildWeatherBean { public int wendu; public String ganmao; public String city; public GrandChildWeatherBean yesterday; public ArrayList forecast; public class GrandChildWeatherBean { public String high; public String fl; public String date; public String low; public String type; public String fx; } public class GrandChildsWeatherBean { public String high; public String fengli; public String fengxiang; public String date; public String low; public String type; } } }
然后将json转化成bean
//content.setText(response); WeatherBean bean = getWeatherFromJson(response);//info为正确编码解析后的json数据,在bean中就可以获取第一级的若干参数 final ChildWeatherBean childWeatherBean = bean.data;//这是第二级的javabean,可获取当下的温度以及一些基本参数 Weather.ChildWeatherBean.GrandChildWeatherBean grandChildWeahterBean = childWeatherBean.yesterday; //yesterday中的内容算是第三级的内容了,下面这句话也是第三级的内容,只不过这是一个List集合,这里就可以直接获取到了。 final ArrayList<Weather.ChildWeatherBean.GrandChildsWeatherBean> grandChildsWeatherBeans = childWeatherBean.forecast; content.setText(grandChildWeahterBean.fx);