인터넷 권한 설정
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>
<?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>
<?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>
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; } }
package kr.android.news.rss; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebSettings; public class NewsRssdetail extends Activity { String title ,link,description; WebView web; protected void onCreate(Bundle save){ super.onCreate(save); 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("<html><body><font size=\"4\">"); text.append(title); text.append("</font><hr size=\"2\" width=\"100%\" noshade>"); text.append("<font size=\"2\">"); text.append(description); text.append("</font><br/>"); text.append("<div align=\"center\"><input type=\"button\" value=\"기사 전문 보기\" onclick=\"location.href='"); text.append(link); text.append("'\"></div>"); text.append("</body></html>"); 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.04.28 |
---|---|
안드로이드 환경설정Preferences , 메뉴 (0) | 2012.04.28 |
Progressbar로 로딩표현 (인터넷로딩) (1) | 2012.04.28 |
안드로이드 서비스 동작중인 서비스 인지 체크 (0) | 2012.04.28 |
안드로이드 프로세스 말끔히 죽이기 (0) | 2012.04.28 |