豆瓣客户端的实现08

来源:互联网 发布:主宰西游装备进阶数据 编辑:程序博客网 时间:2024/06/07 21:01

需求分析

JSON数据解析

快点实现客户端吧

开始

URL地址 http://api.douban.com/book/subject/isbn/7543632608?alt=json

返回的JSON数据

{
    "category": {
        "@scheme": "http://www.douban.com/2007#kind",
        "@term": "http://www.douban.com/2007#book"
    },
    "db:tag": [
        {
            "@count": 126,
            "@name": "片山恭一"
        },
        {
            "@count": 60,
            "@name": "日本"
        },
        {
            "@count": 54,
            "@name": "日本文学"
        },
        {
            "@count": 37,
            "@name": "小说"
        },
        {
            "@count": 31,
            "@name": "满月之夜白鲸现"
        },
        {
            "@count": 14,
            "@name": "爱情"
        },
        {
            "@count": 8,
            "@name": "純愛"
        },
        {
            "@count": 8,
            "@name": "外国文学"
        }
    ],
    "title": {
        "$t": "满月之夜白鲸现"
    },
    "author": [
        {
            "name": {
                "$t": "[日] 片山恭一"
            }
        }
    ],
    "summary": {
        "$t": "那一年,是听莫扎特、钓鲈鱼和家庭破裂的一年。说到家庭破裂,母亲怪自己当初没有找到好男人,父亲则认为当时是被狐狸精迷住了眼,失常的是母亲,但出问题的是父亲……。"
    },
    "link": [
        {
            "@rel": "self",
            "@href": "http://api.douban.com/book/subject/1220562"
        },
        {
            "@rel": "alternate",
            "@href": "http://book.douban.com/subject/1220562/"
        },
        {
            "@rel": "image",
            "@href": "http://img3.douban.com/spic/s1747553.jpg"
        },
        {
            "@rel": "mobile",
            "@href": "http://m.douban.com/book/subject/1220562/"
        }
    ],
    "db:attribute": [
        {
            "$t": "7543632608",
            "@name": "isbn10"
        },
        {
            "$t": "9787543632608",
            "@name": "isbn13"
        },
        {
            "$t": "满月之夜白鲸现",
            "@name": "title"
        },
        {
            "$t": "180",
            "@name": "pages"
        },
        {
            "$t": "豫人",
            "@name": "translator"
        },
        {
            "$t": "[日] 片山恭一",
            "@name": "author"
        },
        {
            "$t": "15.00元",
            "@name": "price"
        },
        {
            "$t": "青岛出版社",
            "@name": "publisher"
        },
        {
            "$t": "平装",
            "@name": "binding"
        },
        {
            "$t": "2005-1",
            "@name": "pubdate"
        }
    ],
    "id": {
        "$t": "http://api.douban.com/book/subject/1220562"
    },
    "gd:rating": {
        "@min": 0,
        "@numRaters": 324,
        "@average": "7.0",
        "@max": 10
    }
}


上源码和效果图




MainActivity.java

public class MainActivity extends Activity {TextView tv_title;TextView tv_summary;LinearLayout ll_loading;@Overrideprotected void onCreate(Bundle savedInstanceState) {requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.book_detail);tv_title = (TextView) this.findViewById(R.id.tv_title);tv_summary = (TextView) this.findViewById(R.id.tv_summary);ll_loading = (LinearLayout) this.findViewById(R.id.ll_book_detail);String isbn = "";fillData(isbn);}private void fillData(String isbn) {new AsyncTask<String, Void, Boolean>() {String title;String summary;String price;@Overrideprotected Boolean doInBackground(String... params) {// http://api.douban.com/book/subject/isbn/7543632608?alt=jsonString path = "http://api.douban.com/book/subject/isbn/7543632608?alt=json";try {URL url = new URL(path);Source source = new Source(url.openConnection());String jsonstr = source.toString();JSONObject jsonobj = new JSONObject(jsonstr);String titlestr = jsonobj.get("title").toString();JSONObject jsontitle = new JSONObject(titlestr);title = jsontitle.get("$t").toString();System.out.println(title);String summarystr = jsonobj.get("summary").toString();if (summarystr != null) {JSONObject jsonsummary = new JSONObject(summarystr);summary = jsonsummary.get("$t").toString();System.out.println(summary);}String attributestr = jsonobj.get("db:attribute").toString();JSONArray attributearray = new JSONArray(attributestr);for (int i = 0; i < attributearray.length(); i++) {JSONObject jsonatt = new JSONObject(attributearray.get(i).toString());if ("price".equals((jsonatt.get("@name").toString()))) {price = jsonatt.get("$t").toString();System.out.println(price);}}return true;} catch (Exception e) {e.printStackTrace();return false;}}@Overrideprotected void onPreExecute() {ll_loading.setVisibility(View.VISIBLE);super.onPreExecute();}@Overrideprotected void onPostExecute(Boolean result) {ll_loading.setVisibility(View.INVISIBLE);if (result) {tv_title.setText(title + "/" + price);tv_summary.setText(summary);} else {Toast.makeText(getApplicationContext(), "查看详情失败", 1).show();}super.onPostExecute(result);}}.execute(isbn);}}

book_detail.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TableLayout        android:layout_width="match_parent"        android:layout_height="match_parent" >        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="标题" />            <TextView                android:id="@+id/tv_title"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />        </TableRow>        <TableRow            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="简介" />            <TextView                android:id="@+id/tv_summary"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />        </TableRow>    </TableLayout>    <LinearLayout        android:id="@+id/ll_book_detail"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@android:color/background_dark"        android:gravity="center"        android:visibility="invisible" >        <ProgressBar            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout></FrameLayout>

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>



源码分析

查看Android中 System.put.println的输出

在Logcat中找标签设置为system.out



0 0