接着上一篇Volley,在使用Volley加载好数据之后,我们肯定不能直接使用这个数据,一般获取的数据都会是Json格式,所以自然而然我们要处理下Json,网络上有很多Json解析库,这里我使用Gson来解析Json,Gson有个特点,就是要将数据的键作为变量封装到一个个实体中,如果值为数组的,变量的类型还必须是集合,然后通过Gson.fromJson来传入数据和实体类,再通过实体类的get方法来获取我们想要的数据,可以说是一个先苦后甜的过程。Gson其实用起来是很方便的,可能第一次觉得有点麻烦,用习惯了就发现它的好处了。废话不多说了,看个简单的Demo:
首先是导入Gson的Jar包,这里我用了gson-2.6.2.jar,讲其放入libs中,然后右键Add As Library即可,下载地址:
http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.6.2%7Cjar
首先看下完整的数据:
{
"rating": {
"max": 10,
"numRaters": 348,
"average": "7.0",
"min": 0
},
"subtitle": "",
"author": [
"[日] 片山恭一"
],
"pubdate": "2005-1",
"tags": [
{
"count": 138,
"name": "片山恭一",
"title": "片山恭一"
},
{
"count": 65,
"name": "日本",
"title": "日本"
},
{
"count": 61,
"name": "日本文学",
"title": "日本文学"
},
{
"count": 38,
"name": "小说",
"title": "小说"
},
{
"count": 32,
"name": "满月之夜白鲸现",
"title": "满月之夜白鲸现"
},
{
"count": 15,
"name": "爱情",
"title": "爱情"
},
{
"count": 8,
"name": "純愛",
"title": "純愛"
},
{
"count": 8,
"name": "外国文学",
"title": "外国文学"
}
],
"origin_title": "",
"image": "https://img1.doubanio.com/mpic/s1747553.jpg",
"binding": "平装",
"translator": [
"豫人"
],
"catalog": "\n ",
"pages": "180",
"images": {
"small": "https://img1.doubanio.com/spic/s1747553.jpg",
"large": "https://img1.doubanio.com/lpic/s1747553.jpg",
"medium": "https://img1.doubanio.com/mpic/s1747553.jpg"
},
"alt": "http://book.douban.com/subject/1220562/",
"id": "1220562",
"publisher": "青岛出版社",
"isbn10": "7543632608",
"isbn13": "9787543632608",
"title": "满月之夜白鲸现",
"url": "http://api.douban.com/v2/book/1220562",
"alt_title": "",
"author_intro": "",
"summary": "那一年,是听莫扎特、钓鲈鱼和家庭破裂的一年。说到家庭破裂,母亲怪自己当初没有找到好男人,父亲则认为当时是被狐狸精迷住了眼,失常的是母亲,但出问题的是父亲……。",
"price": "15.00元"
}
接下来是实体:
/**
* tags
*
* @author yuzhentao
*/
public class Tag {
private String count;
private String name;
private String title;
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
/**
* Book
*
* @author yuzhentao
*/
public class Book {
private String title;
private String publisher;
private String summary;
private ArrayList<Tag> tags;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public ArrayList<Tag> getTags() {
return tags;
}
public void setTags(ArrayList<Tag> tags) {
this.tags = tags;
}
}
接下来是主界面:
/**
* 主界面
*
* @author yuzhentao
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_activity_main).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getData();
}
});
}
/**
* 获取数据
*/
private void getData() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://api.douban.com/v2/book/1220562";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("yuzhentao", "获取成功=" + response);
dealData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("yuzhentao", "获取失败=" + error);
}
});
requestQueue.add(request);
}
/**
* 处理数据
*
* @param result:String
*/
private void dealData(String result) {
Gson gson = new Gson();
Book book = gson.fromJson(result, Book.class);
Log.e("yuzhentao", book.getTitle() + ":" + book.getPublisher() + ":" + book.getTags().size());
}
}
最后是效果,这里直接打印Log:
03-05 10:04:50.719 26924-26924/yuzhentao.gsondemo E/yuzhentao: 满月之夜白鲸现:青岛出版社:8