note

RSS 가져오기(뉴스) 본문

Android/기본

RSS 가져오기(뉴스)

투한 2012. 1. 31. 22:49


실행화면 입니다.




 



권한은 인터넷 권한만 있으면 됩니다.

 


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/mainTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC"
        android:paddingBottom="5px"
        android:paddingTop="5px"
        android:textColor="#000000"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/mainDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC"
        android:paddingBottom="5px"
        android:textColor="#000000"
        android:textSize="12sp" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#FFFFFF"/>

</LinearLayout>


news_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>    
	<WebView
	    android:id="@+id/web"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    /> 
</LinearLayout>



news_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" 
	android:layout_height="50px"
	android:padding="5px"> 
	<TextView 
	    android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:id="@+id/text"
		android:textColor="#000000" 
		android:textSize="15sp"
	    /> 
</LinearLayout>




main javafile
package kr.android.news;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class NewsRssDemo extends ListActivity {
	ArrayList&lt;MyNews&gt; myRss = new ArrayList&lt;MyNews&gt;();
	MyNews myRssProv = new MyNews();
	TextView mainTitle, mainDescription;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		parseXml();
		setListAdapter(new MyListAdapter(this, R.layout.news_list));

		mainTitle = (TextView)findViewById(R.id.mainTitle);
		mainDescription = (TextView)findViewById(R.id.mainDescription);

		mainTitle.setText(myRssProv.title);
		mainDescription.setText(myRssProv.description);
	}
	private void parseXml(){
		InputStream in = null;

		try{
			URL url = new URL("http://www.hani.co.kr/rss/");
			in = url.openStream();

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

			//태크 사이의 공백무(JDK6.0이상부터 오류)
			factory.setIgnoringElementContentWhitespace(true);
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(in);

			//신문사 정보 추출/저장
			Node provider = doc.getElementsByTagName("channel").item(0);

			NodeList prov = provider.getChildNodes();
			for(int j=0; j&lt;prov.getLength();j++){
				Node n = prov.item(j);
				if(n.getNodeName().equals("title")){
					myRssProv.title =n.getFirstChild().getNodeValue();
				}
				if(n.getNodeName().equals("link")){
					myRssProv.link = n.getFirstChild().getNodeValue();
				}
				if(n.getNodeName().equals("description")){
					myRssProv.description = n.getFirstChild().getNodeValue();
					break;
				}
			}
			//기사 추출 &amp; 저장
			NodeList articles = doc.getElementsByTagName("item");

			for(int i=0; i&lt;articles.getLength(); i++){
				MyNews myNews = new MyNews();
				NodeList article = articles.item(i).getChildNodes();
				for(int j=0;j&lt;article.getLength();j++){
					Node n = article.item(j);
					if(n.getNodeName().equals("title")){
						myNews.title = n.getFirstChild().getNodeValue();
					}
					if(n.getNodeName().equals("link")){
						myNews.link = n.getFirstChild().getNodeValue();
					}
					if(n.getNodeName().equals("description")){
						myNews.description = n.getFirstChild().getNodeValue();
					}
				}
				myRss.add(myNews);
			}
		}catch (Exception e){
			e.printStackTrace();
			Toast.makeText(this, "주소 읽기 실패"+e.getMessage(),Toast.LENGTH_SHORT).show();
		}finally{
			try{
				if(in != null) in.close();
			}catch(Exception e){

			}
		}
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(this, NewsRssDetail.class);
		
		intent.putExtra("title", myRss.get(position).title);
		intent.putExtra("link", myRss.get(position).link);
		intent.putExtra("description", myRss.get(position).description);
		
		startActivity(intent);
	}
	//어댑터 클래스
	class MyListAdapter extends BaseAdapter {
		Context context;
		LayoutInflater inflater;
		int layout;
		
		public MyListAdapter(Context context,int layout){
			this.context = context;
			this.layout = layout;
			
			//ListView에서 사용한 View를 정의한 xml를 읽어오기 위해
			//LayoutInflater 객체를 생성
			inflater = LayoutInflater.from(context);
		}
		public int getCount() {
			return myRss.size();
		}
		public Object getItem(int position) {
			return myRss.get(position).title;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = inflater.inflate(layout,parent,false);
			}
			TextView txt = (TextView)convertView.findViewById(R.id.text);
			txt.setText(myRss.get(position).title);
			
			return convertView;
		}
	}

	class MyNews{
		String title;
		String link;
		String description;
	}
}


sub javafile
package kr.android.news;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class NewsRssDetail extends Activity {
	String title, link, description;
	WebView web;
  
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_detail);

		Intent intent = getIntent();

		title = intent.getExtras().getString("title");
		link = intent.getExtras().getString("link");
		description = intent.getExtras().getString("description");

		StringBuffer text = new StringBuffer();
		text.append("");
		text.append(title);
		text.append("
"); text.append(""); text.append(description); text.append("
"); text.append("
"); text.append(""); web=(WebView)findViewById(R.id.web); WebSettings set = web.getSettings(); set.setJavaScriptEnabled(true); set.setBuiltInZoomControls(true); web.loadDataWithBaseURL(null, text.toString(), "text/html", "UTF-8", null); } }