标签:
格式
{"name":"zhangsan", "age":18, "books":[{"name":"金瓶","price":10}, {"name":"红楼梦","price":20}]}
[{"id":"5","version":"5.5","name":"Angry Birds"},
{"id":"6","version":"7.0","name":"Clash of Clans"},
{"id":"7","version":"3.5","name":"Hey Day"}]
private void parseJSONWithJSONObject(String jsonData) {
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.d("MainActivity", "id is " + id);
Log.d("MainActivity", "name is " + name);
Log.d("MainActivity", "version is " + version);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int retcode;
public PhotosInfo data;
public class PhotosInfo {
public String title;
public ArrayList<PhotoInfo> news;
}
/**
* 网络分类信息的封装
*
* 字段名字必须和服务器返回的字段名一致, 方便gson解析
*/
public class NewsData {
public int retcode;
public ArrayList<NewsMenuData> data;
// 侧边栏数据对象
public class NewsMenuData {
public String id;
public String title;
public int type;
public String url;
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 + "]";
}
}
public class GlobalContants {
public static final String SERVER_URL = "http://10.0.2.2:8080/zhbj";
// 获取分类信息的接口
public static final String CATEGORIES_URL = SERVER_URL + "/categories.json";
}
public class NewsCenterPager extends BasePager {
- //只要需要往一个容器添加很多页面,就需要一个arraylist
private ArrayList<BaseMenuDetailPager> mPagers;// 4个菜单详情页的集合
private NewsData mNewsData;
public NewsCenterPager(Activity activity) {
super(activity);
}
@Override
public void initData() {
System.out.println("初始化新闻中心数据....");
tvTitle.setText("新闻");
setSlidingMenuEnable(true);// 打开侧边栏
getDataFromServer();
}
/**
* 从服务器获取数据
*/
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
// 使用xutils发送请求
utils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL,
new RequestCallBack<String>() {
// 访问成功
@Override
public void onSuccess(ResponseInfo 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();
}
});
}
/**
* 解析网络数据
*
* @param result
*/
protected void parseData(String result) {
Gson gson = new Gson();
mNewsData = gson.fromJson(result, NewsData.class);
System.out.println("解析结果:" + mNewsData);
// 刷新测边栏的数据
MainActivity mainUi = (MainActivity) mActivity;
LeftMenuFragment leftMenuFragment = mainUi.getLeftMenuFragment();//获取侧边栏对象
leftMenuFragment.setMenuData(mNewsData);//侧边的方法,使用侧边栏展示数据
// 准备4个菜单详情页
mPagers = new ArrayList<BaseMenuDetailPager>();
mPagers.add(new NewsMenuDetailPager(mActivity,
mNewsData.data.get(0).children));
mPagers.add(new TopicMenuDetailPager(mActivity));
mPagers.add(new PhotoMenuDetailPager(mActivity));
mPagers.add(new InteractMenuDetailPager(mActivity));
setCurrentMenuDetailPager(0);// 设置菜单详情页-新闻为默认当前页
}
/**
* 设置当前菜单详情页
*/
public void setCurrentMenuDetailPager(int position) {
BaseMenuDetailPager pager = mPagers.get(position);// 获取当前要显示的菜单详情页
flContent.removeAllViews();// 清除之前的布局
flContent.addView(pager.mRootView);// 将菜单详情页的布局设置给帧布局
// 设置当前页的标题
NewsMenuData menuData = mNewsData.data.get(position);//获取
mNewsData下数据的第几个页面tvTitle.setText(menuData.title);
pager.initData();// 初始化当前页面的数据
}
}
public class NewsMenuDetailPager extends BaseMenuDetailPager{
private ArrayList<NewsTabData> mNewsTabData;// 页签网络数据
public NewsMenuDetailPager(Activity activity,
ArrayList<NewsTabData> children) {
super(activity);
mNewsTabData = children;
}
@Override
public View initViews() {
return null;
}
}
public abstract class BaseMenuDetailPager {
public Activity mActivity;
public View mRootView;// 根布局对象
public BaseMenuDetailPager(Activity activity) {
mActivity = activity;
mRootView = initViews();
}
/**
* 初始化界面
*/
public abstract View initViews();
/**
* 初始化数据
*/
public void initData() {
}
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4915603.html