今天刚刚学到json解析,看了一整天,大概了解到json就是你通过一个API(我用的聚合数据的API)发送一个请求,接着会收到json数据,比如说天气预报吧,他会给你发送一大段字符串,大概是未来几天的天气情况了什么的,因为这个数据我们想把它规则的展现在手机屏幕上,但是我们得到的json数据未经处理的话,很乱没有美感,也不方便看,就需要我们解析出这些数据并展现在手机屏幕上

cityName = URLEncoder.encode(city,"UTF-8"); 这句话将汉字转化为UTF-8编码

1.首先是要获取json数据,我是直接使用了已经集成的Volley包,获得的数据,Volley又有get和post方法,这里使用get方法

Volley方法获得json数据:

private void volley_get()
{
String cityName;
String city;
@Override
Toast.makeText(MainActivity.this, cityName, Toast.LENGTH_SHORT).show();
String url = "http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&=你申请的Key";
/*
* StringRequest中的第一个参数,是我们获取数据所调用的方法 Method.GET
*第二个参数,是我们获得的API的接口 url
*第三个参数,是当请求成功时所调用的参数
* 第四个参数,是请求失败时所调用的参数
* */
StringRequest request = new StringRequest(Method.GET, url, new Response.Listener<String>()
{
@Override
public void onResponse(String s)
{//参数s就是我们请求成功时返回的json数据
Toast.makeText(MainActivity.this, "加载数据成功", Toast.LENGTH_SHORT).show();
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError volleyError)
{
Toast.makeText(MainActivity.this, "对不起,加载数据失败", Toast.LENGTH_SHORT).show();
}
});
request.setTag("abcGet");
MyApplication.getHttpQueues().add(request);//因为Volley方法就是通过请求队列的方法实现的,所以这里要加入队列 }); }

获得到的天气数据

{"resultcode":"200","reason":"successed!","result":{"sk":{"temp":"37","wind_direction":"西风","wind_strength":"2级","humidity":"49%","time":"18:28"},"today":{"temperature":"30℃~38℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期二","city":"苏州","date_y":"2016年07月26日","dressing_index":"炎热","dressing_advice":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫等清凉夏季服装。","uv_index":"很强","comfort_index":"","wash_index":"较适宜","travel_index":"较适宜","exercise_index":"较适宜","drying_index":""},"future":[{"temperature":"30℃~38℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期二","date":"20160726"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西风微风","week":"星期三","date":"20160727"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期四","date":"20160728"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"南风微风","week":"星期五","date":"20160729"},{"temperature":"28℃~37℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"东风微风","week":"星期六","date":"20160730"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西南风微风","week":"星期日","date":"20160731"},{"temperature":"29℃~39℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"西风微风","week":"星期一","date":"20160801"}]},"error_code":0}

然后在上边的volley_get方法中解析json使其规则的显现

在上边的volley_get方法中 请求成功时调用的参数中实现

 public void onResponse(String s) { //参数s就是我们请求成功时返回的json数据
// tv_1.setText(s);
String weather_city = null;
String weather_date_y = null;
String weather_dressing_advice= null;
String weather_exercise_index= null;
String weather_temperature= null;
String weather_travel_index= null;
String weather_wea= null;
String weather_week= null;
String weather_wind= null;
//
try {
JSONObject jsonObject = new JSONObject(s);
String resultcode = jsonObject.getString("resultcode");
if(resultcode.equals("200")){
JSONObject resultObject=jsonObject.getJSONObject("result");
JSONObject todayObject=resultObject.getJSONObject("today");
weather_city = todayObject.getString("city");
weather_date_y = todayObject.getString("date_y");
weather_dressing_advice = todayObject.getString("dressing_advice");
weather_exercise_index = todayObject.getString("exercise_index");
weather_temperature = todayObject.getString("temperature");
weather_travel_index = todayObject.getString("travel_index");
weather_wea = todayObject.getString("weather");
weather_week = todayObject.getString("week");
weather_wind = todayObject.getString("wind");
}
} catch (JSONException e) {
e.printStackTrace();
}
tv_1.setText("城市 :"+weather_city);
tv_2.setText("日期 :"+weather_date_y);
tv_3.setText("穿衣指数 :"+weather_dressing_advice);
tv_4.setText("锻炼指数 :"+weather_exercise_index);
tv_5.setText("温度 :"+weather_temperature);
tv_6.setText("旅行指数 :"+weather_travel_index);
tv_7.setText("天气 :"+weather_wea);
tv_8.setText("星期 :"+weather_week);
tv_9.setText("风力 :"+weather_wind);
}
}

操作结果:

最新文章

  1. MySQL创建一个用户,指定一个数据库 授权
  2. HTTP 初步知识总结
  3. an introduction to conditional random fields
  4. linux下搭建SVN服务器完全手册【摘抄】
  5. 常见的HTTP错误总结
  6. uiscrollview上的 uipangesturerecognizer冲突
  7. js 保留小数位数
  8. java jdbc使用配置文件连接数据库:
  9. Android 中使用 html 作布局文件
  10. ie6,ie7下设置overflow:auto下滚动条不起作用
  11. ubuntu libtiff-dev
  12. Java Tcp文件传输---转载
  13. WebApi 方法的参数类型总结。
  14. pandas.DataFrame.describe 官方文档翻译percentile_width,percentiles,include, exclude
  15. es5
  16. 自定义函数hello,并注册到hive源码中并重新编译
  17. Apktool下载与安装 windows环境
  18. 如何测试hello world
  19. Fast-R-CNN
  20. java中的关键字、保留字、标识符

热门文章

  1. New Concept English three (49)
  2. cscope配置和使用
  3. java-04类和对象课堂练习
  4. h5废弃的标签和属性及新增的标签和属性
  5. vmem驱动设备
  6. C#获取堆栈信息,输出文件名、行号、函数名、列号等
  7. 笔记:加 ly 不一定是副词
  8. loj 2542 随机游走 —— 最值反演+树上期望DP+fmt
  9. Azure VM的加速网络
  10. 分布式代码管理github