package com.example.xstream;ide import java.net.MalformedURLException; import java.net.URL; import java.util.List;this import android.app.Activity; import android.os.Bundle; import android.widget.ListView;url import com.example.adapter.BookAdapter; import com.example.xsvo.BookItem; import com.example.xsvo.BookRoot; import com.thoughtworks.xstream.XStream;.net public class MainActivity extends Activity {orm 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(); }xml } |