码迷,mamicode.com
首页 > 其他好文 > 详细

xstream解析xml

时间:2016-05-03 22:05:17      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

vo层:

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("result")
public class BookResult {
@XStreamImplicit(itemFieldName="item")
private List<BookItem> item;

 
   

mainactivity.java

package com.example.xstream;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.example.adapter.BookAdapter;
import com.example.xsvo.BookItem;
import com.example.xsvo.BookRoot;
import com.thoughtworks.xstream.XStream;

public class MainActivity extends Activity {

private List<BookItem> mList;
private ListView mListView;
private String url = "http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mListView = (ListView) findViewById(R.id.main_lv);

init();
}
private void init() {
// TODO Auto-generated method stub
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
XStream xs = new XStream();
xs.processAnnotations(BookRoot.class);
BookRoot root;
try {
root = (BookRoot) xs.fromXML(new URL(url));
mList = root.getResult().getItem();
runOnUiThread(new Runnable() {
public void run() {
mListView.setAdapter(new BookAdapter(mList, MainActivity.this));
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}

}

 
   

xstream解析xml

标签:

原文地址:http://www.cnblogs.com/nanze/p/5456467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!