豆瓣客户端的实现08 解析json数据显示在界面上

上效果图 给例子



https://api.douban.com/v2/book/isbn/9787532152902
返回结果
{
    "rating": {
        "max": 10,
        "numRaters": 4,
        "average": "0.0",
        "min": 0
    },
    "subtitle": "",
    "author": [
        "(法) 菲利普·福雷斯特"
    ],
    "pubdate": "2014-7",
    "tags": [
        {
            "count": 14,
            "name": "菲利普·福雷斯特",
            "title": "菲利普·福雷斯特"
        },
        {
            "count": 12,
            "name": "法国",
            "title": "法国"
        },
        {
            "count": 10,
            "name": "小说",
            "title": "小说"
        },
        {
            "count": 8,
            "name": "外国文学",
            "title": "外国文学"
        },
        {
            "count": 3,
            "name": "Forest,Philippe",
            "title": "Forest,Philippe"
        },
        {
            "count": 2,
            "name": "new",
            "title": "new"
        },
        {
            "count": 2,
            "name": "【可信/不可信】",
            "title": "【可信/不可信】"
        },
        {
            "count": 2,
            "name": "叙事",
            "title": "叙事"
        }
    ],
    "origin_title": "Sarinagara",
    "image": "http://img5.douban.com/mpic/s27289319.jpg",
    "binding": "平装",
    "translator": [
        "黄荭"
    ],
    "catalog": "楔子\n一 巴黎\n二 诗人小林一茶的故事\n三 京都\n四 小说家夏目漱石的故事\n五 东京\n六 摄影师山端庸介的故事\n七 神户\n译后记\n浮生一梦,然而……",
    "pages": "264",
    "images": {
        "small": "http://img5.douban.com/spic/s27289319.jpg",
        "large": "http://img5.douban.com/lpic/s27289319.jpg",
        "medium": "http://img5.douban.com/mpic/s27289319.jpg"
    },
    "alt": "http://book.douban.com/subject/25890136/",
    "id": "25890136",
    "publisher": "上海文艺出版社",
    "isbn10": "7532152901",
    "isbn13": "9787532152902",
    "title": "然而",
    "url": "http://api.douban.com/v2/book/25890136",
    "alt_title": "Sarinagara",
    "author_intro": "菲利普?福雷斯特(Philippe Forest),法国作家、文学评论家,一九六二年出生于巴黎,一九八三年毕业于巴黎政治学院,一九九一年获巴黎第四大学文学博士学位。此后,福雷斯特在英国几所大学任教;一九九五年后,到法国南特大学担任文学教授。他还是先锋派文学杂志《原样》和日本现当代文学的研究专家,并为《世界报》书评版和《文学杂志》撰写评论文章,自二〇一一年起,福雷斯特担任《新法兰西杂志》副主编。至今已出版六部小说,其中一九九七年出版的《永恒的孩子》荣获费米娜处女作奖,二〇〇四年出版的《然而》荣获十二月文学奖;二〇一三年,他出版了最新小说《薛定谔的猫》,很快被翻译成中文出版。",
    "summary": "法国先锋派“十二月文学奖”获奖作品\n十二月文学奖原名十一月文学奖,是一个反龚古尔文学奖,由米歇尔?德讷利始创于一九八九年;但由于他不赞成米歇尔?维勒贝克作品《基本粒子》(1998)获奖,被评委会辞退。自此,该先锋派文学奖由奢侈品牌企业家皮埃尔?贝尔热提供赞助,并改为十二月文学奖。在历届获奖者名单里,可以看到许多先锋派作家的名字:让?艾什诺兹、雷吉斯?德布雷、米歇尔?维勒贝克、菲利普?福雷斯特、让-菲利普?图森……\n--------------------------------------------------------------------------------------------------------\n在小女儿夭折之后,作者借着追溯三位日本艺术家的故事,完成了自身艰难的心路历程。小林一茶同样痛失过幼儿,他借助诗歌来排遣内心的痛苦,写下许多洞彻人世的俳句,包括这首《然而》;日本现代小说之父夏目漱石也失去过幼小的女儿,在小说里描述这种失女之痛;还有第一位拍摄长崎原子弹爆炸罹难者的摄影师山端庸介,他用镜头记录垂死的幼童。本书荣获法国十二月文学奖,是法国“自我虚构”(auto-fiction)小说的扛鼎之作。",
    "price": "29.00"
}

上源码

public class MainActivity extends Activity {

    private EditText et;
    private Button bt;
    private Handler hd;
    private ProgressDialog mpd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	Log.e("Book", "MainActivity------------------------onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.button1);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            	mpd=new ProgressDialog(MainActivity.this);
                mpd.setMessage("请稍候,正在读取信息...");
                mpd.show();

                String urlstr="https://api.douban.com/v2/book/isbn/9787532152902";
                Log.i("OUTPUT",urlstr);
                //扫到ISBN后,启动下载线程下载图书信息
                new DownloadThread(urlstr).start();
                Log.e("Book", "MainActivity------------------------onClick");
            }
        });
        hd=	new Handler(){
            @Override
            public void handleMessage(Message msg) {
            	Log.e("Book", "MainActivity------------------------handleMessage");
                super.handleMessage(msg);
                BookInfo book= (BookInfo)msg.obj;

                //进度条消失
                mpd.dismiss();

                Intent intent=new Intent(MainActivity.this,BookView.class);
                //Bundle bd=new Bundle();
                //bundle.putSerializable(key,object);
                //bd.putSerializable(BookInfo.class.getName(),book);
                //intent.putExtras(bd);
                intent.putExtra(BookInfo.class.getName(),book);
                startActivity(intent);
                Log.e("Book", "MainActivity------------------------handleMessage结束");
            }
        };
        
        Log.e("Book", "MainActivity------------------------onCreate结束");
    }
    
    
   

    private class DownloadThread extends Thread
    {
        String url=null;
        public DownloadThread(String urlstr)
        {
            url=urlstr;
        }
        public void run()
        {
        	Log.e("Book", "MainActivity------------------------run");
            String result=Util.Download(url);
            Log.i("OUTPUT", "download over");
            BookInfo book=new Util().parseBookInfo(result);
            Log.i("OUTPUT", "parse over");
            Log.i("OUTPUT",book.getSummary()+book.getAuthor());
            //给主线程UI界面发消息,提醒下载信息,解析信息完毕

            Message msg=Message.obtain();
            msg.obj=book;
            hd.sendMessage(msg);
            Log.e("Book", "MainActivity------------------------run结束");
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入图书ISBN号" />

      
    

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" />

</LinearLayout>
public class BookView extends Activity {
    private Intent intent;
    private TextView title,author,publisher,date,isbn,summary;
    private ImageView cover;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	Log.e("Book", "BookView------------------------onCreate");
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.bookview);

        title=(TextView)findViewById(R.id.bookview_title);
        author=(TextView)findViewById(R.id.bookview_author);
        publisher=(TextView)findViewById(R.id.bookview_publisher);
        date=(TextView)findViewById(R.id.bookview_publisherdate);
        isbn=(TextView)findViewById(R.id.bookview_isbn);
        summary=(TextView)findViewById(R.id.bookview_summary);
        cover=(ImageView)findViewById(R.id.bookview_cover);

        intent=getIntent();
        //BookInfo book=(BookInfo) getIntent().getSerializableExtra(BookInfo.class.getName());
        BookInfo book=(BookInfo)intent.getParcelableExtra(BookInfo.class.getName());

        title.setText(book.getTitle());
        author.setText(book.getAuthor());
        publisher.setText(book.getPublisher());
        date.setText(book.getPublishDate());
        isbn.setText(book.getISBN());
        summary.setText(book.getSummary());
        cover.setImageBitmap(book.getBitmap());
        Log.e("Book", "BookView------------------------onCreate结束");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/bookview_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center|bottom|left|right|top"
            android:gravity="center_horizontal"
            android:text="测试显示样本"
            android:textColor="#000000"
            android:textSize="30px" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10px"
            android:orientation="horizontal"
            android:paddingLeft="20px" >

            <ImageView
                android:id="@+id/bookview_cover"
                android:layout_width="150px"
                android:layout_height="170px" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20px"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/bookview_author"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="作者"
                    android:textSize="25px" />

                <TextView
                    android:id="@+id/bookview_publisher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5px"
                    android:text="出版社"
                    android:textSize="25px" />

                <TextView
                    android:id="@+id/bookview_publisherdate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5px"
                    android:text="时间"
                    android:textSize="25px" />

                <TextView
                    android:id="@+id/bookview_isbn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5px"
                    android:text="ISBN"
                    android:textSize="25px" />
            </LinearLayout>
        </LinearLayout>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10px"
            android:gravity="center_horizontal"
            android:text="内容介绍"
            android:textSize="28px" />

        <TextView
            android:id="@+id/bookview_summary"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10px"
            android:paddingLeft="20px"
            android:paddingRight="20px"
            android:text="具体内容" />
    </LinearLayout>

</ScrollView>
public class BookInfo implements Parcelable {

    private String mTitle="";
    private Bitmap mBitmap;
    private String mAuthor="";
    private String mPublisher="";
    private String mPublishDate="";
    private String mISBN="";
    private String mSummary="";

    public void setTitle(String Title)
    {
        mTitle=Title;
    }
    public void setBitmap(Bitmap bitmap)
    {
        mBitmap=bitmap;
    }
    public void setAuthor(String Author)
    {
        mAuthor=Author;
    }
    public void setISBN(String ISBN)
    {
        mISBN=ISBN;
    }
    public void setPublishDate(String PublishDate)
    {
        mPublishDate=PublishDate;
    }
    public void setPublisher(String Publisher)
    {
        mPublisher=Publisher;
    }
    public void setSummary(String Summary)
    {
        mSummary=Summary;
    }

    public String getTitle()
    {
         return mTitle;
    }
    public Bitmap getBitmap()
    {
        return mBitmap;
    }
    public String getAuthor()
    {
        return mAuthor;
    }

    public String getISBN()
    {
        return mISBN;
    }
    public String getPublishDate()
    {
        return mPublishDate;
    }
    public String getPublisher()
    {
        return mPublisher;
    }
    public String getSummary()
    {
        return mSummary;
    }

    public static final Parcelable.Creator<BookInfo> CREATOR = new Creator<BookInfo>() {
        public BookInfo createFromParcel(Parcel source) {
        	Log.e("Book", "BookInfo------------------------createFromParcel");
            BookInfo bookInfo = new BookInfo();
            bookInfo.mTitle = source.readString();
            bookInfo.mBitmap = source.readParcelable(Bitmap.class.getClassLoader());
            bookInfo.mAuthor = source.readString();
            bookInfo.mPublisher = source.readString();
            bookInfo.mPublishDate = source.readString();
            bookInfo.mISBN = source.readString();
            bookInfo.mSummary = source.readString();
            return bookInfo;
        }
        public BookInfo[] newArray(int size) {
            return new BookInfo[size];
        }
    };
    public int describeContents() {
        return 0;
    }
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mTitle);
        dest.writeParcelable(mBitmap, flags);
        dest.writeString(mAuthor);
        dest.writeString(mPublisher);
        dest.writeString(mPublishDate);
        dest.writeString(mISBN);
        dest.writeString(mSummary);
    }

}
public class Util {
	   public static String Download(String urlstr)
	   {
		   Log.e("Book", "Util------------------------Download");
	       String result="";
	       try{
	           URL url=new URL(urlstr);
	           URLConnection connection =url.openConnection();

	           String line;
	           BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
	           while ((line = in.readLine()) != null) {
	               result += line;
	           }
	       }catch (Exception e) {
	           e.printStackTrace();
	       }
	       return  result;
	   }

	    public BookInfo parseBookInfo(String str)
	    {
	        BookInfo info=new BookInfo();
	        try{
	            JSONObject mess=new JSONObject(str);
	            info.setTitle(mess.getString("title"));
	            info.setBitmap(DownloadBitmap(mess.getString("image")));
	            info.setAuthor(parseJSONArraytoString(mess.getJSONArray("author")));
	            info.setPublisher(mess.getString("publisher"));
	            info.setPublishDate(mess.getString("pubdate"));
	            info.setISBN(mess.getString("isbn13"));
	            info.setSummary(mess.getString("summary"));

	        }catch (Exception e) {
	            e.printStackTrace();
	        }
	        return info;
	    }

	     public Bitmap DownloadBitmap(String bmurl)
	     {
	    	 Log.e("Book", "Util------------------------DownloadBitmap");
	         Bitmap bm=null;
	         InputStream is =null;
	         BufferedInputStream bis=null;
	         try{
	             URL  url=new URL(bmurl);
	             URLConnection connection=url.openConnection();
	             bis=new BufferedInputStream(connection.getInputStream());
	             bm= BitmapFactory.decodeStream(bis);
	         }catch (Exception e){
	             e.printStackTrace();
	         }
	         finally {
	             try {
	                 if(bis!=null)
	                     bis.close();
	                 if (is!=null)
	                     is.close();
	             }catch (Exception e){
	                 e.printStackTrace();
	             }
	         }
	         return bm;
	     }
	    public String parseJSONArraytoString (JSONArray arr)
	    {
	    	Log.e("Book", "Util------------------------parseJSONArraytoString");
	        StringBuffer str =new StringBuffer();
	        for(int i=0;i<arr.length();i++)
	        {
	            try{
	                str=str.append(arr.getString(i)).append(" ");
	            }catch (Exception e){
	                e.printStackTrace();
	            }
	        }
	        return str.toString();
	    }
	}
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<activity
            android:name="cn.vlang.book.BookView"
            android:label="BookView" >
            
        </activity>

OK

结束 下一节代码分析








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值