您的位置:首页 > 移动开发 > Android开发

Android获取服务器Json数据与Json的解析

2015-12-20 12:01 447 查看
由于有第三方框架的存在,从服务器获取Json数据以及解析变得非常非常的简单。

1 获取Json数据

第一个第三方框架是xUtils的HttpUtils:

在Global包中定义了一些全局静态变量类,用于储存访问路径,在这里电脑分配给我模拟器的IP为:192.168.56.1:8080

则路径定义是:

package com.example.zhihuibj.global;
public class GlobalContants {

public static final String SERVICE_URL="http://192.168.56.1:8080/zhbj/";
public static final String CATEGORIES_URL=SERVICE_URL+"categories.json";//获取分类信息

}


使用Get方法获取网络数据使用HttpUtils:

/*
* 获取网络数据
*/
private void GetDataFromService() {
HttpUtils utils=new HttpUtils();
utils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL,
new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result= responseInfo.result;
System.out.println("返回结果"+result);
parseData(result);
}

@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(mAcitivty, msg, Toast.LENGTH_SHORT).show();
error.printStackTrace();
System.out.println("解析错误!!!");
}
});

}


所有的数据Json数据会储存在result中,并在自定义的parseData()中进行解析。

获取数据就是这么简单。

2 Json数据解析

对于数据的解析使用Google提供的 Jar包: gson-2.3.1.jar

这个jar包提供了一个只需要传入用于储存Json数据的自定义类,即可自动完成所有数据的解析。

下面是服务器端的Json数据:

{
"data": [
{
"children": [
{
"id": 10007,
"title": "北京",
"type": 1,
"url": "/10007/list_1.json"
},
{
"id": 10006,
"title": "中国",
"type": 1,
"url": "/10006/list_1.json"
},
{
"id": 10008,
"title": "国际",
"type": 1,
"url": "/10008/list_1.json"
},
{
"id": 10010,
"title": "体育",
"type": 1,
"url": "/10010/list_1.json"
},
{
"id": 10091,
"title": "生活",
"type": 1,
"url": "/10091/list_1.json"
},
{
"id": 10012,
"title": "旅游",
"type": 1,
"url": "/10012/list_1.json"
},
{
"id": 10095,
"title": "科技",
"type": 1,
"url": "/10095/list_1.json"
},
{
"id": 10009,
"title": "军事",
"type": 1,
"url": "/10009/list_1.json"
},
{
"id": 10093,
"title": "时尚",
"type": 1,
"url": "/10093/list_1.json"
},
{
"id": 10011,
"title": "财经",
"type": 1,
"url": "/10011/list_1.json"
},
{
"id": 10094,
"title": "育儿",
"type": 1,
"url": "/10094/list_1.json"
},
{
"id": 10105,
"title": "汽车",
"type": 1,
"url": "/10105/list_1.json"
}
],
"id": 10000,
"title": "新闻",
"type": 1
},
{
"id": 10002,
"title": "专题",
"type": 10,
"url": "/10006/list_1.json",
"url1": "/10007/list1_1.json"
},
{
"id": 10003,
"title": "组图",
"type": 2,
"url": "/10008/list_1.json"
},
{
"dayurl": "",
"excurl": "",
"id": 10004,
"title": "互动",
"type": 3,
"weekurl": ""
}
],
"extend": [
10007,
10006,
10008,
10014,
10012,
10091,
10009,
10010,
10095
],
"retcode": 200
}


对于这个数据自定义一个储存类,以C语言的角度来讲,就是用一个结构体来储存数据:

package com.example.zhihuibj.domain;

import java.util.ArrayList;

/*
* 网络分类信息封装
*
* 字段必须和服务器返回的字段相同,方便gson解析
*/
public class NewsData {

public int retcode;
public ArrayList<NewsMenuData> data;

public class NewsMenuData{

@Override
public String toString() {
return "NewsMenuData [id=" + id + ", title=" + title + ", type=" + type + ", url=" + url + ", children="
+ children + "]";
}
public String id;
public String title;
public int type;
public String url;
//public String url1;
public ArrayList<NewsTabData> children;

}

public class NewsTabData{

public String id;
public String title;
public int type;
public String url;

}

}


最后调用parasData解析数据即可:

/*
* 解析网络数据
*/
protected void parseData(String result) {
Gson gson =new Gson();
NewsData data = gson.fromJson(result, NewsData.class);//直接填入类名

System.out.println("解析结果"+data);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: