为了账号安全,请及时绑定邮箱和手机立即绑定

Gson解析第三方提供Json数据(天气预报,新闻等)

标签:
Android

之前都是自己写后台,自己的服务器提供数据给客户端,

最近在看第三方的数据接口,访问其他网站提供的信息;比如,我们可能自己收集的数据相当有限,但是网上提供了很多关于天气预报、新闻、星座运势、身份证号、车辆违章、健康医疗、快递查询、ip查询、翻译等的api接口,基本返回数据为类型json和xml

我就喜欢简单便捷的东西,在这解析一下第三方新闻的接口返回的json数据;

我喜欢用谷歌提供的Gson,感觉比JSON去解析要简单,方便,快捷;当然了阿里巴巴提供的fastjson也是极好的,在这只用gson解析了(废话似乎多了点)。

①先看一下我要解析的第三方的数据:(图片看不清可以拖动图片到新的页面标签中看哦~)


②然后从上面可以得到json数据的key值。


③因为Gson是基于对象的,所以我们要将这些 “键值”建立一个实体类

首先可以看到最外层是三个键值 error_code ,reason,result 然后result中为一个json数组,那么我们将json数组中的数据单独抽出一个对象来;

示例代码如下:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

package com.zml.pojo;

 

import java.util.List;

 

/**

 * @author 郑明亮

 * @Time:2016-3-18 上午10:28:55

 * @version 1.0

 */

public class NewsJson   {

    String   error_code;

    String reason;

    List<news>   result;

     

    public NewsJson(String error_code, String   reason, List<news> result) {

        super();

        this.error_code   = error_code;

        this.reason   = reason;

        this.result   = result;

    }

 

    public NewsJson() {

        super();

        //   TODO Auto-generated constructor stub

    }

 

    public String getError_code() {

        return error_code;

    }

 

    public void setError_code(String error_code) {

        this.error_code   = error_code;

    }

 

    public String getReason() {

        return reason;

    }

 

    public void setReason(String reason) {

        this.reason   = reason;

    }

 

    public List<news> getResult() {

        return result;

    }

 

    public void setResult(List<news> result) {

        this.result   = result;

    }

 

    @Override

    public String toString() {

        return "NewsJson [error_code=" + error_code + ", reason=" + reason

                +   ", result=" + result + "]";

    }

     

}

</news></news></news></news>

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

package com.zml.pojo;

/**

 * @author 郑明亮

 * @Time:2016-3-18 上午10:27:13

 * @version 1.0

 */

public class News {

    String ctime;

    String title;

    String picUrl;

    String url;

     

     

    public News(String ctime, String tittle, String   picUtl, String url) {

        super();

        this.ctime   = ctime;

        this.title   = tittle;

        this.picUrl   = picUtl;

        this.url   = url;

    }

 

 

    public News() {

        super();

        //   TODO Auto-generated constructor stub

    }

 

 

    public String getCtime() {

        return ctime;

    }

 

 

    public void setCtime(String ctime) {

        this.ctime   = ctime;

    }

 

 

    public String getTittle() {

        return title;

    }

 

 

    public void setTittle(String tittle) {

        this.title   = tittle;

    }

 

 

    public String getPicUtl() {

        return picUrl;

    }

 

 

    public void setPicUtl(String picUtl) {

        this.picUrl   = picUtl;

    }

 

 

    public String getUrl() {

        return url;

    }

 

 

    public void setUrl(String url) {

        this.url   = url;

    }

 

 

    @Override

    public String toString() {

        return "News [ctime=" + ctime + ", tittle=" + title + ", picUtl="

                +   picUrl + ", url=" + url + "]";

    }

}

④然后就是进行解析了,解析方法我在之前的博文中已经介绍了,如果没看的可以,先看看;

                  传送门 Andorid之Gson解析Json数据

在这直接将方法列出来了:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

public static <t>   T getObjectData(String jsonString, Class<t> type) {

 

    T t = null;

    try {

        Gson   gson = new Gson();

        t   = gson.fromJson(jsonString, type);

    } catch (JsonSyntaxException e) {

        //   TODO Auto-generated catch block

        e.printStackTrace();

    }

    return t;

}</t></t>

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/**

     * 将输入流转换为byte[]

     *

     * @param is

     * @return

     */

    public static byte[] IsToByte(InputStream is) {

 

        ByteArrayOutputStream   bos = new ByteArrayOutputStream();

        byte buffer[] = new byte[1024];

        int len = 0;

        try {

            while ((len = is.read(buffer)) != -1) {

                bos.write(buffer,   0, len);

 

            }

 

        }   catch (IOException   e) {

            //   TODO Auto-generated catch block

            e.printStackTrace();

            return null;

        }   finally {

            try {

                bos.flush();

                bos.close();

                is.close();

            }   catch (IOException   e) {

                //   TODO Auto-generated catch block

                e.printStackTrace();

            }

 

        }

 

        return bos.toByteArray();

    }

⑤测试解析方法:

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

package com.zml.pojo.test;

 

import static org.junit.Assert.*;

 

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

 

import org.junit.Test;

 

import com.zml.pojo.NewsJson;

import com.zml.utils.GsonTools;

 

/**

 * @author 郑明亮

 * @Time:2016年3月18日 上午10:35:39

 * @version 1.0

 */

public class TestGsonAPI   {

 

    @Test

    public void test() {

        try {

            URL   url = new URL("http://api.avatardata.cn/TechNews/Query?key=5e3bedcfa2714e36a3e46dd2efce00d9&page=1&rows=10");

            HttpURLConnection   connection = (HttpURLConnection) url.openConnection();

//            String data = connection.getContentType();

                String   dataString = new String(GsonTools.IsToByte(connection.getInputStream()),"utf-8");

                NewsJson   newsJson = GsonTools.getObjectData(dataString, NewsJson.class);

                System.out.println(newsJson.toString());

            System.out.println(dataString);

        }   catch (Exception   e) {

            //   TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

 

}

数据太多了,只截图了一部分数据:(图片看不清可以拖动图片到新的页面标签中看哦~)

原文链接:http://www.apkbus.com/blog-725636-61281.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消