您的位置:首页 > 理论基础 > 计算机网络

RxVolley进行网络请求(get方式),获取json数据

2017-05-20 20:21 453 查看
RxVolley 是一个基于
Volley
的网络请求库
,项目地址:
https://github.com/kymjs/RxVolley

1、添加依赖:

 compile
'com.kymjs.rxvolley:rxvolley:1.1.4'
//在app 下的build.gradle 里

2、聚合数据申请微信精选接口,获取APP-key

请求数据是url网址  


RxVolley.get(url, new HttpCallback() {		//url为要请求的网址
//成功返回json数据--onSuccess为重写方法
@Override
public void onSuccess(String t) {
Toast.makeText(getApplicationContext(),
"成功",Toast.LENGTH_SHORT).show();
L.i("json"+t);		//t为请求成功时获得的json数据

parseJson(t);	//解析json数据
}
});


3、定义解析json数据的方法

查看json数据的格式   


//1、声明JSONObject 对象
JSONObject jsonObject=new JSONObject(t);
//2、获取JSONObject 数据
JSONObject jsonResult=jsonObject.getJSONObject("result");
//3、通过Object对象获取到JSONArray(list数据)
JSONArray jsonArray=jsonResult.getJSONArray("list");
//4、根据key值获取到对象的value--一个一个获取
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object= (JSONObject) jsonArray.get(i);
String title=object.getString("title");
String source=object.getString("source");
String imgUrl=object.getString("firstImg");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐