Gson解析库使用笔记

                                                                           返回主页

写了一个小DEMO总结一下Gson解析库的使用。 相对于原生SDK解析而言确实方便了很多。

效果图

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gsondemo.MainActivity"
tools:ignore="MergeRootFrame" >


<TextView 
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="30sp"
    android:text="@string/gson"
    />
</RelativeLayout>

很直观,添加了一个文本区域

MainActivity.java

package com.example.gsondemo;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.Response.Listener;


/**
 * http://api.douban.com/v2/book/24536403
* @author Administrator
*
*/
public class MainActivity extends Activity {

private String url = "http://api.douban.com/v2/book/24536403";

private TextView text1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text1 = (TextView)findViewById(R.id.text1);
    getData();
}

private void getData() {

    StringRequest request = new StringRequest(url, new Listener<String>() {

        @Override
        public void onResponse(String arg0) {
            Log.i("info", arg0);
            dealData(arg0);
        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            // TODO Auto-generated method stub

        }
    });
    new Volley();
    Volley.newRequestQueue(getApplicationContext()).add(request);
}

private void dealData(String result) {
    Gson gson = new Gson();
    Book book = gson.fromJson(result, Book.class);
    String resultString = book.getTitle() + ":" + book.getPublisher() + ":" +
             book.getTags().size();
    Log.i("info", book.getTitle() + ":" + book.getPublisher() + ":" +
     book.getTags().size());
    text1.setText(resultString);
}

}

URL为豆瓣读书的某书籍的json数据接口,大概是这种样子

{ “rating”: { “max”: 10, “numRaters”: 591, “average”: “7.8”, “min”: 0 }, “subtitle”: “代码的未来”, “author”: [ “[日] 松本行弘” ], “pubdate”: “2013-6”, “tags”: [ { “count”: 485, “name”: “编程”, “title”: “编程” }, { “count”: 370, “name”: “松本行弘”, “title”: “松本行弘” }, { “count”: 258, “name”: “计算机”, “title”: “计算机” }, { “count”: 228, “name”: “编程语言”, “title”: “编程语言” }, { “count”: 133, “name”: “计算机科学”, “title”: “计算机科学” }, { “count”: 112, “name”: “Programming”, “title”: “Programming” }, { “count”: 105, “name”: “软件开发”, “title”: “软件开发” }, { “count”: 104, “name”: “IT”, “title”: “IT” } ],

Book.java

package com.example.gsondemo;

import java.util.ArrayList;

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;
}
}

一个Book的POJO,用于获取json数据,注意属性名称应当与json数据标签一一对应

tag.java

package com.example.gsondemo;

public class Tag {

private String countString;
private String name;
private String title;
public String getCountString() {
    return countString;
}
public void setCountString(String countString) {
    this.countString = countString;
}
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;
}
}

tag类是Book类中定义的泛型tags的实例

demo在Android4.4中编译成功