XmlPullParser 测试2

package com.gyarmy.newstest1;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.gyarmy.newstest1.domain.NewsItem;
import com.gyarmy.newstest1.utils.InputStreamTool;

import android.os.Bundle;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;

public class MainActivity extends Activity {

	private String path = "http://www.xxx.com/test/news.xml";
	private NewsItem item;
	private List<NewsItem> itemList = new ArrayList<NewsItem>();
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        new Thread(){
        	public void run() {
        		try {
					URL url = new URL(path);
					HttpURLConnection conn = (HttpURLConnection)url.openConnection();
					conn.setConnectTimeout(2000);
					conn.setRequestMethod("GET");
					int code = conn.getResponseCode();
					if(200 == code)
					{
						InputStream in = conn.getInputStream();
						//String data = InputStreamTool.decodeData(in);
						//System.out.println(data);
						XmlPullParser parser = Xml.newPullParser();
						parser.setInput(in, "utf-8");
						int type = parser.getEventType();
						
						/*
						 * 
						 *  <item>
								<title>军报评徐才厚</title>
								<description>人死账不消 反腐步不停,支持,威武,顶,有希望了。</description>
								<image>http://c.cheshijie.com/test/a.jpg</image>
								<type>1</type>
								<comment>163</comment>
								</item>
						 * 
						 */
						
						while(type!=parser.END_DOCUMENT){
							if(type == parser.START_TAG){
								if("item".equals(parser.getName())){
									item = new NewsItem();
								}else if("title".equals(parser.getName())){
									item.setTitle(parser.nextText());
								}else if("description".equals(parser.getName())){
									item.setDescription(parser.nextText());
								}else if("image".equals(parser.getName())){
									item.setImage(parser.nextText());
								}else if("type".equals(parser.getName())){
									item.setType(parser.nextText());
								}else if("comment".equals(parser.getName())){
									item.setComment(parser.nextText());
								}
								
							}else if(type == parser.END_TAG){
								if(item!=null){
									System.out.println(item);
									itemList.add(item);
								}
							}
							
							type=parser.next();
						}
						
						
						
						
					}else{
						System.out.println("Error");
					}
					
					
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
        		
        		
        		
        	};
        	
        }.start();
        
    }
    
    
    
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

原文链接: XmlPullParser 测试2 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-120.html )

发表评论

0则评论给“XmlPullParser 测试2”