把json的文件放在服务器中,然后创建一个全局的类方便读取,使用xUtils方便访问网络数据,同时使用Gjson来帮助解析Json数据
服务器的路径数据ClobalContants.java
package com.ldw.news.global;
/*
* 定义一个全局的参数
*/
public class ClobalContants {
//json文件的tomcat路径,10.0.2.2都可以访问
public static final String SERVER_URL = "http://10.0.2.2:8080/zhbj";
public static final String CATEGORIES_URL = SERVER_URL + "/categories.json";// 获取分类信息的接口
}
NewsCenterPager.java中来解析数据,方便之后加载数据
package com.ldw.news.base.impl;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.ldw.news.base.BasePager;
import com.ldw.news.domain.NewsData;
import com.ldw.news.global.ClobalContants;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
/**
* 新闻中心
*
* @author Kevin
*
*/
public class NewsCenterPager extends BasePager {
//private ArrayList<BaseMenuDetailPager> mPagers;// 4个菜单详情页的集合
private NewsData mNewsData;
public NewsCenterPager(Activity activity) {
super(activity);
}
@Override
public void initData() {
System.out.println("初始化设置数据....");
tvTitle.setText("设置");
btnMenu.setVisibility(View.GONE);// 隐藏菜单按钮
setSlidingMenuEnable(false);// 关闭侧边栏
TextView text = new TextView(mActivity);
text.setText("设置");
text.setTextColor(Color.RED);
text.setTextSize(25);
text.setGravity(Gravity.CENTER);
// 向FrameLayout中动态添加布局
flContent.addView(text);
//从服务器中获取数据
getDataFromServer();
}
/*
* 从服务器中获取数据,使用xUtils实现
*/
private void getDataFromServer(){
HttpUtils utils = new HttpUtils();
//参数是String获取到的是String
utils.send(HttpMethod.GET, ClobalContants.CATEGORIES_URL, new RequestCallBack<String>(){
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//获取到返回的结果
String result = (String) responseInfo.result;
System.out.println("返回结果:" + result);
parseData(result);
}
//访问失败
@Override
public void onFailure(HttpException error, String msg) {
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT)
.show();
//打印错误信息
error.printStackTrace();
}
});
}
/*
* 解析网络数据
*/
protected void parseData(String result) {
//使用Gson解析
Gson gson = new Gson();
//第一个参数是数据,第二个参数是要转化成什么对象
mNewsData = gson.fromJson(result, NewsData.class);
System.out.println("解析结果:" + mNewsData);
}
}
NewsData.java中解析json数据并添加打印
package com.ldw.news.domain;
import java.util.ArrayList;
/*
* 网络分类信息的封装
* 各个字段的名名字需要一直,方便Gson解析
*/
public class NewsData {
public int retcode;//返回值
public ArrayList<NewsMenuData> data;//字段数据
//data下面又有一组4个数据
// 侧边栏数据对象
public class NewsMenuData {
public String id;
public String title;
public int type;
public String url;
//解析children的数据
public ArrayList<NewsTabData> children;
@Override
public String toString() {
return "NewsMenuData [title=" + title + ", children=" + children
+ "]";
}
}
// 新闻页面下11个子页签的数据对象
public class NewsTabData {
public String id;
public String title;
public int type;
public String url;
@Override
public String toString() {
return "NewsTabData [title=" + title + "]";
}
}
@Override
public String toString() {
return "NewsData [data=" + data + "]";
}
}
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
}
Json数据类型如下