简易新闻客户端
来源:互联网 发布:淘宝店铺导航栏颜色 编辑:程序博客网 时间:2024/06/01 08:57
需要网络权限<uses-permission android:name="android.permission.INTERNET" />
package com.example.net;import android.app.Activity;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.util.Xml;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import com.loopj.android.image.SmartImageView;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.List;public class NewsClient extends Activity { Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { ListView list= (ListView) findViewById(R.id.list); list.setAdapter(new MyAdapter()); } }; private List<News> newsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news_client); parseXml(); } private void parseXml(){ new Thread(){ @Override public void run() { String path="http://192.168.0.101/news/news.xml"; /* <?xml version="1.0" encoding="utf-8"?> <news> <new> <title>标题标题</title> <detail>详细内容</detail> <image>http://192.168.0.101/news/images/image1.jpg</image> <comment>10001</comment> </new> <new> <title>标题标题</title> <detail>详细内容。</detail> <image>http://192.168.0.101/news/images/image1.jpg</image> <comment>10001</comment> </new> </news> */ try { URL url=new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); if(conn.getResponseCode()==200){ InputStream is=conn.getInputStream(); XmlPullParser xml = Xml.newPullParser(); xml.setInput(is,"utf-8"); int type=xml.getEventType(); News news=null; while(type!=XmlPullParser.END_DOCUMENT){ switch (type){ case XmlPullParser.START_TAG: if("news".equals(xml.getName())){ newsList=new ArrayList<News>(); }else if("new".equals(xml.getName())){ news=new News(); }else if("title".equals(xml.getName())){ news.setTitle(xml.nextText()); }else if("detail".equals(xml.getName())){ news.setDetail(xml.nextText()); } else if("image".equals(xml.getName())){ news.setImageurl(xml.nextText()); } else if("comment".equals(xml.getName())){ news.setComment(xml.nextText()); } break; case XmlPullParser.END_TAG: if("new".equals(xml.getName())){ newsList.add(news); } break; } type=xml.next(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } handler.sendEmptyMessage(0); } }.start(); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return newsList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v=null; if(convertView==null){ v=View.inflate(NewsClient.this,R.layout.list,null); }else{ v=convertView; } //SmartImageView为使用了别人的类库github中搜索smart-image-view复制源文件到项目 SmartImageView imageView= (SmartImageView) v.findViewById(R.id.iv_photo); TextView tv_title= (TextView) v.findViewById(R.id.tv_title); TextView tv_detail= (TextView) v.findViewById(R.id.tv_detail); TextView tv_comment= (TextView) v.findViewById(R.id.tv_comment); News news=newsList.get(position); //直接将图片下载添加上去 imageView.setImageUrl(news.getImageurl()); tv_title.setText(news.getTitle()); tv_detail.setText(news.getDetail()); tv_comment.setText(news.getComment()+"条评论"); return v; } }}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.net.NewsClient"> <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.loopj.android.image.SmartImageView android:layout_centerVertical="true" android:id="@+id/iv_photo" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignParentLeft="true" android:src="@drawable/image1" /> <TextView android:layout_marginTop="10dp" android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_photo" android:singleLine="true" android:textSize="16sp" android:text="山东平邑县委书记县长被免职" /> <TextView android:id="@+id/tv_detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_photo" android:lines="2" android:layout_below="@id/tv_title" android:textSize="16sp" android:textColor="#55000000" android:text="连续发生生产安全事故,常务副县长和分管副县长也被免职" /> <TextView android:id="@+id/tv_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_detail" android:layout_alignParentRight="true" android:textColor="#88ff0000" android:text="10001条评论" /></RelativeLayout>
news类
package com.example.net;/** * Created by joy on 2015/12/29. */public class News { private String title; private String detail; private String imageurl; private String comment; public News(String title, String detail, String imageurl, String comment) { this.title = title; this.detail = detail; this.imageurl = imageurl; this.comment = comment; } public News() { } public String getTitle() { return title; } public String getDetail() { return detail; } public String getImageurl() { return imageurl; } public String getComment() { return comment; } public void setTitle(String title) { this.title = title; } public void setDetail(String detail) { this.detail = detail; } public void setImageurl(String imageurl) { this.imageurl = imageurl; } public void setComment(String comment) { this.comment = comment; }}
0 0
- 简易新闻客户端
- 简易新闻客户端案例
- 简易新闻客户端
- 案例-----简易新闻客户端
- Android简易新闻客户端
- 简易新闻客户端(生成列表)
- 安卓开发之简易新闻客户端
- Android---网络编程之简易新闻客户端
- 网络编程案例简易新闻客户端
- android开发笔记之网络编程—简易新闻客户端
- 使用mvc模型实现简易新闻客户端功能(本地)
- 网络编程小案例之简易新闻客户端
- 安卓之旅第十一站--简易新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- getParameter取不到值
- C# 实现HTTP POST请求与接收响应
- 怎样使用easyUI
- 《java并发编程实战》读书笔记——基础知识
- Android中XML文件的使用
- 简易新闻客户端
- 【数据结构】链栈的基本操作
- 6. 隐藏 index.php
- 矩阵解压(UVa11082)
- C++, Java和C#的编译过程解析
- Lua封装延时执行函数
- 源码探索系列16---初篇结尾---那个被RecyclerView替代的Listview
- JS生成二维码(兼容各种浏览器及中文)
- RxJava练习(2)--时间间隔输出字符串