Android_网络操作

 向服务器请求数据:

  两种请求方式:GET 与 POST

Android_网络操作

  get一般用于从服务器获取数据,post一般用于向服务器提交数据;

如何通过get获取数据:

  注意:在android4.0以后的版本,主线程(UI线程)不在支持网络请求,原因大概是影响主线程,速度太慢,容易卡机,所以需要开启新的线程请求数据;

 //HttpUrlConnection
        /**
         * 1.实例化一个url对象
         * 2.获取HttpUrlConnection对象
         * 3.设置请求连接属性
         * 4.获取响应码,判断是否连接成功
         * 5.读取输入流并解析
         */
        //参数:你要访问的接口地址
        new Thread(){


            @Override
            public void run() {
                try {
                    URL url = new URL("http://www.imooc.com/api/teacher?type=2");
                    HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                    coon.setRequestMethod("GET");
                    coon.setReadTimeout(6000);
                    //获取响应码
                    if(coon.getResponseCode() == 200){
                        //获取输入流
                        InputStream in = coon.getInputStream();
                        byte[] b = new byte[1024*512];
                        int len = 0;
                        //建立缓存流,保存所读取的字节数组
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        while ((len = in.read(b)) > -1){
                            baos.write(b,0,len);
                        }
                        String msg = baos.toString();
}.start

解析json普通数据

  解析的数据

{
    "status": 1,
    "data": {
        "title": "Tony老师聊shell——环境变量配置文件",
        "author": "Tony",
        "content": "本课程是《Tony老师聊shell》系列课程的第三篇,为你带来常用的环境变量配置文件的使用。"
    },
    "msg": "成功"
}

使用JSONObject解析:

 1                          //JSON数据的解析:
 2                         JSONObject obj = new JSONObject(msg);
 3                         int status = obj.getInt("status");
 4                         String msg2 = obj.getString("msg");
 5                         Log.e("TAG", status + "   " + msg2);
 6                         //解析data数组先获取data对象
 7                         JSONObject jsonObject = obj.getJSONObject("data");
 8                         String title = jsonObject.getString("title");
 9                         String author = jsonObject.getString("author");
10                         String content = jsonObject.getString("content");
11                         Log.e("TAG", "标题:" + title + ",作者:" + author + ",内容:" + content);

使用GSON快速解析数组数据

数据:

 1 {
 2     "status": 1,
 3     "data": [{
 4         "id": 1,
 5         "name": "Tony老师聊shell——环境变量配置文件",
 6         "picSmall": "http://img.mukewang.com/55237dcc0001128c06000338-300-170.jpg",
 7         "picBig": "http://img.mukewang.com/55237dcc0001128c06000338.jpg",
 8         "description": "为你带来shell中的环境变量配置文件",
 9         "learner": 12312
10     }, {
11         "id": 2,
12         "name": "数学知识在CSS动画中的应用",
13         "picSmall": "http://img.mukewang.com/55249cf30001ae8a06000338-300-170.jpg",
14         "picBig": "http://img.mukewang.com/55249cf30001ae8a06000338.jpg",
15         "description": "数学知识与CSS结合实现酷炫效果",
16         "learner": 45625
17     }, {
18         "id": 3,
19         "name": "Oracle数据库开发必备利器之PL/SQL基础",
20         "picSmall": "http://img.mukewang.com/5523711700016d1606000338-300-170.jpg",
21         "picBig": "http://img.mukewang.com/5523711700016d1606000338.jpg",
22         "description": "Oracle数据库高级开发必备的基础。",
23         "learner": 41236
24     }, {
25         "id": 4,
26         "name": "Android见证消息推送时刻进阶篇",
27         "picSmall": "http://img.mukewang.com/551e470500018dd806000338-300-170.jpg",
28         "picBig": "http://img.mukewang.com/551e470500018dd806000338.jpg",
29         "description": "Android消息推送就在眼前,Come on",
30         "learner": 45456
31     }, {
32         "id": 5,
33         "name": "Avalon探索之旅基础教程——复杂绑定",
34         "picSmall": "http://img.mukewang.com/551de0570001134f06000338-300-170.jpg",
35         "picBig": "http://img.mukewang.com/551de0570001134f06000338.jpg",
36         "description": "前端迷您MVVM框架,Avalon复杂绑定属性篇。",
37         "learner": 56556
38     }, {
39         "id": 6,
40         "name": "Android-Service系列之断点续传下载",
41         "picSmall": "http://img.mukewang.com/552640c300018a9606000338-300-170.jpg",
42         "picBig": "http://img.mukewang.com/552640c300018a9606000338.jpg",
43         "description": "想升职加薪么?本章课程你值得拥有",
44         "learner": 48996
45     }, {
46         "id": 7,
47         "name": "JUnit—Java单元测试必备工具",
48         "picSmall": "http://img.mukewang.com/551b92340001c9f206000338-300-170.jpg",
49         "picBig": "http://img.mukewang.com/551b92340001c9f206000338.jpg",
50         "description": "Java单元测试利器!",
51         "learner": 13210
52     }, {
53         "id": 8,
54         "name": "细说Java多线程之内存可见性",
55         "picSmall": "http://img.mukewang.com/5518c3d7000175af06000338-300-170.jpg",
56         "picBig": "http://img.mukewang.com/5518c3d7000175af06000338.jpg",
57         "description": "用两种方式实现内存可见性",
58         "learner": 15051
59     }, {
60         "id": 9,
61         "name": "CSS动画实用技巧",
62         "picSmall": "http://img.mukewang.com/551b98ae0001e57906000338-300-170.jpg",
63         "picBig": "http://img.mukewang.com/551b98ae0001e57906000338.jpg",
64         "description": "教你使用CSS实现惊艳的动画效果!",
65         "learner": 15210
66     }, {
67         "id": 10,
68         "name": "C++远征之起航篇",
69         "picSmall": "http://img.mukewang.com/550b86560001009406000338-300-170.jpg",
70         "picBig": "http://img.mukewang.com/550b86560001009406000338.jpg",
71         "description": "C++亮点尽在其中",
72         "learner": 84545
73     }],
74     "msg": "成功"
75 }

使用GSON快速获取data数组数据

               //1.创建Gson对象
                        Gson gson = new Gson();
                        //参数1:满足json对象格式的字符串
                        String data = obj.getString("data");
                        //Data实体类用来存放data数组的数据
                        ArrayList<Data> list = gson.fromJson(data, new TypeToken<ArrayList<Data>>() {
                        }.getType());
                        for (int i = 0; i < list.size(); i++) {
                            Data a = list.get(i);
                            Log.e(TAG, "id =" + a.getId() + "name=" + a.getName());
                        }

   使用GSON可以快速的获取json数据中数组的数据,而不需要一个一个的获取jsonobject对象,然后再一个一个的获取数据.

Android_网络操作

上一篇:《开发板 —— USB的host、device、otg模式》


下一篇:数据库Oracle强化练习之多表查询