联网请求数据:Android篇

这篇文章主要回顾之前的MYangtzeu App教程的联网请求方法和json解析套路,主要目的是实现左侧菜单数据的联网获取。

1、先上请求地址实体类Constants.java

package com.vitoyan.myangtzeu.utils;

/**
* 作者:Vito-Yan
* 作用:常量类,配置联网请求地址
*/
public class Constants { /**
* 联网请求的ip和端口
*/
public static final String BASE_URL = "http://10.155.106.108:9090/web_home";
// public static final String BASE_URL = "http://192.168.191.1:8080/web_home"; // public static final String BASE_URL = "http://10.0.2.2:8080/web_home"; /**
* 新闻中心的网络地址
*/
public static final String HOME_PAGER_URL = BASE_URL+"/static/api/news/categories.json";
}

2、解析Json后的实体类HomePageBean.java

package com.vitoyan.myangtzeu.pojo;

import java.util.List;

/**
* 作者:Vito-Yan
* 作用:新闻中心解析后对应的数据
*/
public class HomePagerBean { private List<DetailPagerData> data;
private List extend;
private int retcode; public List<DetailPagerData> getData() {
return data;
} public void setData(List<DetailPagerData> data) {
this.data = data;
} public List getExtend() {
return extend;
} public void setExtend(List extend) {
this.extend = extend;
} public int getRetcode() {
return retcode;
} public void setRetcode(int retcode) {
this.retcode = retcode;
} @Override
public String toString() {
return "NewsCenterPagerBean2{" +
"data=" + data +
", extend=" + extend +
", retcode=" + retcode +
'}';
} public static class DetailPagerData{
private int id;
private String title;
private int type;
private String url;
private String url1;
private String dayurl;
private String excurl;
private String weekurl;
private List<ChildrenData> children; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getUrl1() {
return url1;
} public void setUrl1(String url1) {
this.url1 = url1;
} public String getDayurl() {
return dayurl;
} public void setDayurl(String dayurl) {
this.dayurl = dayurl;
} public String getExcurl() {
return excurl;
} public void setExcurl(String excurl) {
this.excurl = excurl;
} public String getWeekurl() {
return weekurl;
} public void setWeekurl(String weekurl) {
this.weekurl = weekurl;
} public List<ChildrenData> getChildren() {
return children;
} public void setChildren(List<ChildrenData> children) {
this.children = children;
} @Override
public String toString() {
return "DetailPagerData{" +
"id=" + id +
", title='" + title + '\'' +
", type=" + type +
", url='" + url + '\'' +
", url1='" + url1 + '\'' +
", dayurl='" + dayurl + '\'' +
", excurl='" + excurl + '\'' +
", weekurl='" + weekurl + '\'' +
", children=" + children +
'}';
} public static class ChildrenData{
private int id;
private String title;
private int type;
private String url; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} @Override
public String toString() {
return "ChildrenData{" +
"id=" + id +
", title='" + title + '\'' +
", type=" + type +
", url='" + url + '\'' +
'}';
}
}
}
}

3、使用xUtils3联网请求数据,使用前自行下载modules

/**
* 使用xUtils3联网请求数据
*/
private void getDataFromNet() { RequestParams params = new RequestParams(Constants.HOME_PAGER_URL);
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
long endTime = SystemClock.uptimeMillis();
long passTime = endTime - startTime; LogUtil.e("xUtils3--passTime==" + passTime); LogUtil.e("使用xUtils3联网请求成功==" + result); //缓存数据
CacheUtils.putString(context,Constants.HOME_PAGER_URL,result); //解析json数据和显示数据
processData(result); //设置适配器 } @Override
public void onError(Throwable ex, boolean isOnCallback) {
LogUtil.e("使用xUtils3联网请求失败==" + ex.getMessage());
} @Override
public void onCancelled(CancelledException cex) {
LogUtil.e("使用xUtils3-onCancelled==" + cex.getMessage());
} @Override
public void onFinished() {
LogUtil.e("使用xUtils3-onFinished");
}
}); }

4、解析Json,这里使用Gson解析

/**
* 解析json数据:1,使用系统的API解析json;2,使用第三方框架解析json数据,例如Gson,fastjson
*
* @param json
* @return
*/
private HomePagerBean parsedJson(String json) {
// Gson gson = new Gson();
// HomePagerBean bean = gson.fromJson(json,HomePagerBean.class);
return new Gson().fromJson(json, HomePagerBean.class);
}

5、解析Json数据和显示数据

/**
* 解析json数据和显示数据
*
* @param json
*/
private void processData(String json) { HomePagerBean bean = parsedJson(json);
// HomePagerBean2 bean = parsedJson2(json);
// String title = bean.getData().get(0).getChildren().get(1).getTitle(); // LogUtil.e("使用Gson解析json数据成功-title==" + title);
String title2 = bean.getData().get(0).getChildren().get(1).getTitle();
LogUtil.e("使用Gson解析json数据成功HomePagerBean2-title2-------------------------==" + title2);
//给左侧菜单传递数据
data = bean.getData(); MainActivity mainActivity = (MainActivity) context;
//得到左侧菜单
LeftmenuFragment leftmenuFragment = mainActivity.getLeftmenuFragment(); //添加详情页面
detaiBasePagers = new ArrayList<>();
detaiBasePagers.add(new NewsMenuDetailPager(context,data.get(0)));//新闻详情页面
detaiBasePagers.add(new News1MenuDetailPager(context));//新闻1详情页面
detaiBasePagers.add(new News2MenuDetailPager(context));//新闻2详情页面
detaiBasePagers.add(new News3MenuDetailPager(context));//新闻3详情页面
detaiBasePagers.add(new News4MenuDetailPager(context));//新闻4详情页面 //把数据传递给左侧菜单
leftmenuFragment.setData(data); }

6、在LeftmenuFragment中接受数据

/**
* 接收数据
* @param data
*/
public void setData(List<HomePagerBean.DetailPagerData> data) {
this.data = data;
for(int i=0;i<data.size();i++){
LogUtil.e("title=="+data.get(i).getTitle());
} //设置适配器
adapter = new LeftmenuFragmentAdapter();
listView.setAdapter(adapter); //设置默认页面
swichPager(prePosition); }

联网请求数据:Android篇

上一篇:【原创】uwsgi中多进程+多线程原因以及串行化accept() - thunder_lock说明


下一篇:Python 解leetcode:48. Rotate Image