• XMl的解析简单的方法


    首先需要jia包
    xstream-1.4.7.jar
    Vo类
    
    package com.zld.day06_03zk3demo.bean;
    
    import java.io.Serializable;
    import java.util.List;
    
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    import com.thoughtworks.xstream.annotations.XStreamImplicit;
    
    /*@XStreamAlias("DATA")*/
    @XStreamAlias("DATA")
    最外面的那层
    public class XmlBean implements Serializable{
    /*@XStreamImplicit(itemFieldName="item")*/ @XStreamImplicit(itemFieldName="item") public List<Item>item; public class Item implements Serializable{ public String title; public String link; public String imgs; public String description; public String copyright; public String pubDate; public String comments; } }

     

    Mainactivity
    
    package com.zld.day06_03zk3demo;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    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 com.thoughtworks.xstream.XStream;
    import com.zld.day06_03zk3demo.adapter.MyAdapter;
    import com.zld.day06_03zk3demo.bean.XmlBean;
    import com.zld.day06_03zk3demo.bean.XmlBean.Item;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    public class MainActivity extends Activity {
        private ListView lv;
        private Handler handler;
        private List<Item>list;
        
    	@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            list=new ArrayList<XmlBean.Item>();
            handler=new Handler(){
            	@Override
            	public void handleMessage(Message msg) {
            		super.handleMessage(msg);
            		list=(List<Item>) msg.obj;
            		MyAdapter adapter=new MyAdapter(list, MainActivity.this);
            		lv.setAdapter(adapter);
            	}
            };
            init();
        }
    
    	private void init() {
    		lv = (ListView) findViewById(R.id.main_lv);
    		requestData();
    		lv.setOnItemClickListener(new OnItemClickListener() {
    			@Override
    			public void onItemClick(AdapterView<?> parent, View view,
    					int position, long id) {
    				Intent it=new Intent(MainActivity.this,DetailsActivity.class);
    				it.putExtra("item", list.get(position));
    				startActivity(it);
    			}
    		});
    	}
    
    	private void requestData() {
    		new Thread(){
    			public void run() {
    				HttpClient client=new DefaultHttpClient();
    				HttpGet get=new HttpGet("http://www.sciencenet.cn/xml/iphoneInterface.aspx?type=news&nums=20&pass=");
    				try {
    					HttpResponse response = client.execute(get);
    					if(response.getStatusLine().getStatusCode()==200){
    						String xml = EntityUtils.toString(response.getEntity(), "utf-8");
    						XStream xs=new XStream();
    						xs.processAnnotations(XmlBean.class);
    						XmlBean xmlBean = (XmlBean) xs.fromXML(xml);
    						Message msg=Message.obtain();
    						msg.what=1;
    						msg.obj=xmlBean.item;
    						handler.sendMessage(msg);
    					}
    				} catch (ClientProtocolException e) {
    					e.printStackTrace();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			};
    		}.start();
    	}
    
    }
    

      

     

  • 相关阅读:
    简介&目录
    Lucas 定理
    扩展欧几里得算法(exgcd)
    【做题记录】CF23B Party
    【做题记录】CF1375D Replace by MEX
    【做题记录】CF194B Square
    SPFA
    dijkstra
    CSP-J&S 2020 游记
    中国剩余定理(CRT)
  • 原文地址:https://www.cnblogs.com/jsonfan/p/5326644.html
Copyright © 2020-2023  润新知