pull解析解析月考题
MainActivity.class:
package com.example.map_new_two; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ListView; public class MainActivity extends Activity { private String url="http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml"; private ListView list_view; private List<Item> list=new ArrayList<Item>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_view=(ListView) findViewById(R.id.list_view); new Thread(){ public void run() { init(); } }.start(); list_view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent it=new Intent(getApplicationContext(),HomeActivity.class); startActivity(it); } }); } private void init() { // TODO Auto-generated method stub HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet(url); try { HttpResponse httpResponse=httpClient.execute(httpGet); int len=httpResponse.getStatusLine().getStatusCode(); if(len==200){ HttpEntity httpEntity=httpResponse.getEntity(); InputStream input=httpEntity.getContent(); XmlPullParserFactory factory=XmlPullParserFactory.newInstance(); XmlPullParser parser=factory.newPullParser(); parser.setInput(input, "utf-8"); int exenttype=parser.getEventType(); String name=""; Item item = null; while(exenttype!=XmlPullParser.END_DOCUMENT){ switch (exenttype) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: name = parser.getName(); if("item".equals(name)){ item=new Item(); } break; case XmlPullParser.TEXT: String text=parser.getText(); if("id".equals(name)){ item.setId(text); }else if("catalog".equals(name)){ item.setCatalog(text); Log.i("TAG","fffffffrrrrrrrrrrrrrrrrrrrrrrrffff"+text); } break; case XmlPullParser.END_TAG: name = parser.getName(); if("item".equals(name)){ list.add(item); item=null; } name=""; break; case XmlPullParser.END_DOCUMENT: break; default: break; } exenttype=parser.next(); } } runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Mybase base=new Mybase(list, getApplicationContext()); list_view.setAdapter(base); } }); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }
vo类:
package com.example.map_new_two; public class Item { private String id; private String catalog; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCatalog() { return catalog; } public void setCatalog(String catalog) { this.catalog = catalog; } @Override public String toString() { return "Item [id=" + id + ", catalog=" + catalog + "]"; } }
适配器:bitmap解析图片:
package com.example.map_new_two; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.TextView; public class Mybase extends BaseAdapter{ private List<Item> base; private Context context; public Mybase(List<Item> base, Context context) { super(); this.base = base; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return base.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return base.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub Viehoder vh; if(arg1==null){ vh=new Viehoder(); arg1=LayoutInflater.from(context).inflate(R.layout.item,null); vh.texView1=(TextView) arg1.findViewById(R.id.textView1); arg1.setTag(vh); }else{ vh=(Viehoder) arg1.getTag(); } vh.texView1.setText(base.get(arg0).getCatalog()); return arg1; } class Viehoder{ TextView texView1; } }