Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - 기본
 - Spring
 - Java
 - Menu
 - 어노테이션
 - 안드로이드
 - 예외처리
 - layout
 - 메서드
 - Android
 - paint
 - 국제화
 - 배열
 - HTML
 - oracle
 - 에러페이지
 - Eclips
 - Graphic
 - 이클립스
 - struts2
 - AWT
 - mybatis
 - 오버로딩
 - JavaScript
 - OGNL
 - JSP
 - 생성자
 - 메소드
 - 클래스
 - 전화걸기
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
note
RSS 가져오기(뉴스) 본문
실행화면 입니다.
권한은 인터넷 권한만 있으면 됩니다.
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<MyNews> myRss = new ArrayList<MyNews>();
	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<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;
				}
			}
			//기사 추출 & 저장
			NodeList articles = doc.getElementsByTagName("item");
			for(int i=0; i<articles.getLength(); i++){
				MyNews myNews = new MyNews();
				NodeList article = articles.item(i).getChildNodes();
				for(int j=0;j<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);
	}
}
'Android > 기본' 카테고리의 다른 글
| Android Preferences 읽고 쓰기(환경설정) (0) | 2012.02.01 | 
|---|---|
| 안드로이드 환경설정Preferences , 메뉴 (0) | 2012.01.31 | 
| Progressbar로 로딩표현 (인터넷로딩) (0) | 2012.01.31 | 
| 안드로이드 서비스 동작중인 서비스 인지 체크 (0) | 2012.01.30 | 
| 안드로이드 프로세스 말끔히 죽이기 (0) | 2012.01.28 |