目录

  • 模块1 Android UI设计
    • ● 版本与程序
    • ● 学习目标
    • ● 本章PPT
    • ● 任务1-1 线性布局应用示例
    • ● 任务1-2 帧布局应用示例
    • ● 任务1-3 表格布局应用示例
    • ● 任务1-4 相对布局应用示例
    • ● 任务1-5 网格布局应用示例
    • ● 任务1-6 约束布局应用示例
    • ● 任务1-7 文本标签组件示例
    • ● 任务1-8 制作登陆界面
    • ● 任务1-9 进度条ProgressBar应用示例
    • ● 任务1-10 单选按钮与复选按钮
    • ● 任务1-11 简单列表示例
    • ● 任务1-12 ListActivity示例
    • ● 任务1-13 带有图片的列表(选做)
    • ● 任务1-14 没有数据的ListView(选做)
    • ● 任务1-15 改进的列表(选做)
    • ● 实战演练——生肖背后的故事
  • 模块2   Activity与多个用户界面
    • ● 学习目标
    • ● 本章PPT
    • ● 任务2-1 从Activity启动另一个Activity示例
    • ● 任务2-2 传递数据到第二个Activity示例
    • ● 实战演练-生肖的界面跳转
    • ● 任务2-3 消息提示Toast示例
    • ● 任务2-4 消息对话框示例
    • ● 任务2-5 选项菜单应用示例
    • ● 任务2-6 上下文菜单应用示例
    • ● 实战演练——BMI体质指数计算器
  • 模块3   多媒体播放与录制
    • ● 学习目标
    • ● 本章PPT
    • ● 任务3-1 音乐播放器, 播放项目资源中的音乐
    • ● 任务3-2 音乐播放器, 播放SD卡中的音乐
    • ● 任务3-3 应用MediaPlayer设计视频播放器
    • ● 任务3-4 应用VidioView设计视频播放器
    • ● 任务3-5 录音示例
    • ● 任务3-6 拍照示例
    • ● 实战演练——音乐播放器
  • 模块4   广播与服务
    • ● 学习目标
    • ● 本章PPT
    • ● 任务4-1 消息广播程序示例
    • ● 任务4-2 系统通知服务示例
    • ● 任务4-3 广播和时钟服务示例
    • ● 任务4-4 调用系统功能拨打电话
    • ● 任务4-5 后台音乐服务示例
    • ● 实战演练——播放后台音乐
  • 模块5   数据存储
    • ● 学习目标
    • ● 本章PPT
    • ● 任务5-1 内部存储文件示例
    • ● 任务5-2 SD文件示例
    • ● 任务5-3 JSON数据示例
    • ● 任务5-4 SharedPreferences示例
    • ● 任务5-5 创建与删除数据库示例
    • ● 任务5-6 数据库记录的操作示例
    • ● 实战演练——掌上日记本
  • 模块6   图像和动画
    • ● 学习目标
    • ● 本章PPT
    • ● 任务6-1 绘制几何图形示例
    • ● 任务6-2 补间动画示例
    • ● 任务6-3 属性动画示例
    • ● 任务6-4 ImageView图像浏览示例
    • ● 任务6-5 ImageSwitcher展示相册示例
    • ● 任务6-6 GridView展示相册示例
    • ● 任务6-7 游戏中触屏事件示例
    • ● 实战演练——图片与动画
  • 模块7   网络编程
    • ● 学习目标
    • ● 本章PPT
    • ● 任务7-1 应用WebView的对象浏览网页
    • ● 任务7-2 从Web服务器读取图像文件
    • ● 任务7-3 读取JSON数据
    • ● 任务7-4 解析JSON数据
    • ● 实战演练——城市天气预报
  • 模块8   分享动漫
    • ● 学习目标
    • ● 制作步骤PPT
    • ● 示范视频
    • ● 学生作品展示
  • 模块9   天气预报
    • ● 学习目标
    • ● 制作步骤PPT
    • ● 示范视频
  • 课程资源
    • ● 学生资源包
    • ● 网站地址
任务7-4 解析JSON数据

项目短视频


操作示范如下(时长27:36)



【例7-4】解析网络JSON数据

不用另外设计,使用【例7-3】的布局文件activity_volley.xml。

准备自己搭建一个服务器,放一个JSON文件jsonData.json,或者去外网访问JSON数据,例如

http://www.weather.com.cn/data/cityinfo/101010100.html


jsonData.json文件

{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"18℃","temp2":"31℃","weather":"多云转阴", "img1":"d1.gif", "img2":"n1.gif", "ptime":"08:00"}}

该文件可以尝试以下链接:

http://pan-yz.chaoxing.com/external/m/file/634801254428688384

Javabean文件 Weatherinfo.java源代码如下:

package com.example.chap07;

//练习 javabean, 项目也可以不用 javabean 

public class Weatherinfo {

    /** JSON数据样本

     * city : 北京

     * cityid : 101010100

     * temp1 : 15℃

     * temp2 : 5℃

     * weather : 多云

     * img1 : d1.gif

     * img2 : n1.gif

     * ptime : 08:00

     */


    private String city;

    private String cityid;

    private String temp1;

    private String temp2;

    private String weather;

    private String img1;

    private String img2;

    private String ptime;

    public  Weatherinfo(){


    }

    public  Weatherinfo(String city, String cityid, String temp1,

    String temp2, String weather,String img1, String img2,String ptime)

    {

        this.city = city;

        this.cityid = cityid;

        this.temp1 = temp1;

        this.temp2 = temp2;

        this.weather = weather;

        this.img1 = img1;

        this.img2 = img2;

        this.ptime = ptime;

    }

    @Override

    public String toString() {

        return "WeatherInfo [city=" + city + ", cityid=" + cityid + ", temp1="

                + temp1 + ", temp2=" + temp2 + ", weather=" + weather

                + ", ptime=" + ptime + "]";

    }

}

主控制程序WeatherActivity.java的源代码如下:

package com.example.chap07;


import android.os.Bundle;


import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;


import com.android.volley.NetworkResponse;

import com.android.volley.ParseError;

import com.android.volley.RequestQueue;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.HttpHeaderParser;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.Volley;


import org.json.JSONException;

import org.json.JSONObject;


import java.io.UnsupportedEncodingException;

/*JSON数据样本

       { "weatherinfo": {

            "city": "北京",

            "cityid": "101010100",

            "temp1": "18℃",

            "temp2": "31℃",

            "weather": "多云转阴",

            "img1": "d1.gif",

            "img2": "n1.gif",

            "ptime": "08:00"

        }

    }*/


//【例7-4】应用Volley框架解析JSON数据。

public class WeatherActivity extends AppCompatActivity

        implements View.OnClickListener {

    Button Btn;

    TextView txt;

    String url = "http://www.weather.com.cn/data/cityinfo/101010100.html";


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_volley);

        Btn = (Button) findViewById(R.id.btn);

        txt = (TextView) findViewById(R.id.txt);

        Btn.setOnClickListener(this);

    }


    @Override

    public void onClick(View v) {

        JsonObjectRequest stringRequest= new JsonObjectRequest(

                //第 1个参数,目标服务器的URL地址

                url,

                //第2个参数,JSONObject

                null,

                //第3个参数,服务器响应成功的回调

                new Response.Listener<JSONObject>(){

                    @Override

                    public void onResponse(JSONObject response) {

                        try {

                            JSONObject mjson=response.getJSONObject("weatherinfo");   //Jason数据的键值对的键key

                            String city=mjson.getString("city");

                            String cityid=mjson.getString("cityid");

                            String temp1=mjson.getString("temp1");

                            String temp2=mjson.getString("temp2");

                            String weather=mjson.getString("weather");

                            String img1=mjson.getString("img1");

                            String img2=mjson.getString("img2");

                            String ptime=mjson.getString("ptime");

                            String s="[city=" + city + ", cityid=" + cityid + ", temp1="

                                    + temp1 + ", temp2=" + temp2 + ", weather=" + weather

                                    + ", ptime=" + ptime + "]";

                            txt.setText(s);

                            //加入一个 Javabean 的练习

                            Weatherinfo bean = new Weatherinfo(city, cityid, temp1,

                                    temp2, weather,img1, img2, ptime);

                            Log.d("TAG", bean.toString());


                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

                    }

                },

                //第4个参数,服务器响应失败的回调

                new Response.ErrorListener(){

                    @Override

                    public void onErrorResponse(VolleyError volleyError) {

                        txt.setText("网页访问失败");

                    }

                }){

            @Override

            protected Response<JSONObject> parseNetworkResponse(

                    NetworkResponse response) {

                try {

                    JSONObject jsonObject = new JSONObject(

                            new String(response.data, "UTF-8"));//将默认的ISO-8859-1转换为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)) ;

                }

            }

        };


        RequestQueue mQueue = Volley.newRequestQueue(this);

        mQueue.add(stringRequest);

    }

}



打开项目配置文件AndroidManifest.xml,检查网络权限,确保工程要启动的Activity类名为

WeatherActivity