简易新闻客户端案例
来源:互联网 发布:手机淘宝店铺实名认证 编辑:程序博客网 时间:2024/06/05 21:01
运行界面如下:
实现这个案例需要在project视图下的app文件夹下的libs文件夹下导入三个包,如下图所示:
(1)首先我们在main_activity布局文件中创建以下代码:
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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=".MainActivity" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/loading" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="invisible"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在加载信息..." /> </LinearLayout> <ListView android:id="@+id/lv_news" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout></LinearLayout>
(2)创建List View布局,Item.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="65dp"> <com.loopj.android.image.SmartImageView android:id="@+id/siv_icon" android:layout_width="80dp" android:layout_height="60dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"> </com.loopj.android.image.SmartImageView> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_toRightOf="@id/siv_icon" android:ellipsize="end" android:maxLength="20" android:singleLine="true" android:text="我是标题" android:textColor="#000000" android:textSize="18sp" /> <TextView android:id="@+id/tv_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_toRightOf="@id/siv_icon" android:ellipsize="end" android:maxLength="16" android:maxLines="1" android:text="我是描述" android:textColor="#99000000" android:textSize="14sp" /> <TextView android:id="@+id/tv_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="5dp" android:layout_marginRight="10dp" android:text="评论" android:textColor="#99000000" android:textSize="12sp" /></RelativeLayout>
(3)创建一个entity文件夹存放News Info类
package com.example.bz0209.news.entity;/** * Created by Administrator on 2017/5/18. */public class NewsInfo { private String iconPath; private String title; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getIconPath() { return iconPath; } public void setIconPath(String iconPath) { this.iconPath = iconPath; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public long getComment() { return comment; } public void setComment(long comment) { this.comment = comment; } public int getType() { return type; } public void setType(int type) { this.type = type; } private String description; private int type; private long comment;}
(4)编写工具类NewsInfoService解析XML文件得到List对象,并编写config.xml获取serverurl的值
NewsInfoService代码如下:
package com.example.bz0209.news.dao;import android.util.Xml;import com.example.bz0209.news.entity.NewsInfo;import org.xmlpull.v1.XmlPullParser;import java.io.InputStream;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/5/22. */public class NewsInfoService { public static List<NewsInfo> getNewsInfos(InputStream is){ XmlPullParser parser= Xml.newPullParser(); try{ parser.setInput(is,"utf-8"); int type=parser.getEventType(); List<NewsInfo> newsInfos=null; NewsInfo newsInfo=null; while (type!=XmlPullParser.END_DOCUMENT){ switch (type){ case XmlPullParser.START_TAG: if("news".equals(parser.getName())){ newsInfos=new ArrayList<NewsInfo>(); }else if("newsInfo".equals(parser.getName())){ newsInfo=new NewsInfo(); }else if("icon".equals(parser.getName())){ String icon=parser.nextText(); newsInfo.setIconPath(icon); }else if("title".equals(parser.getName())){ String title=parser.nextText(); newsInfo.setTitle(title); }else if("content".equals(parser.getName())){ String description=parser.nextText(); newsInfo.setDescription(description); }else if("type".equals(parser.getName())){ String newsType=parser.nextText(); newsInfo.setType(Integer.parseInt(newsType)); }else if("comment".equals(parser.getName())){ String comment=parser.nextText(); newsInfo.setComment(Long.parseLong(comment)); } break; } type=parser.next(); } return newsInfos; }catch(Exception e){ e.printStackTrace(); return null; } }}
config.xml文件代码如下
<?xml version="1.0" encoding="utf-8"?><resources> <string name="serverurl">http://10.51.30.166:8080/newInfo.xml</string></resources>
(5)编写用户交互代码,并进行List View的适配器编码,并用AysncHttpClient获取服务器的Xml文件
package com.example.bz0209.news;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.view.ViewParent;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import cz.msebera.android.httpclient.Header;import com.example.bz0209.news.dao.NewsInfoService;import com.example.bz0209.news.entity.NewsInfo;import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.AsyncHttpResponseHandler;import com.loopj.android.image.SmartImageView;import java.io.ByteArrayInputStream;import java.util.List;import java.util.Objects;import static android.R.id.content;public class MainActivity extends AppCompatActivity { private ListView lv_news; private LinearLayout loading; private List<NewsInfo> newsInfos; private class NewsAdapter extends BaseAdapter{ public int getCount(){ return newsInfos.size(); } @Override public View getView(int i, View view, ViewGroup viewGroup) { return null; } public View getView(int position, View convertView, ViewParent parent){ View view=View.inflate(MainActivity.this,R.layout.item,null); SmartImageView siv= (SmartImageView) view.findViewById(R.id.siv_icon); TextView tv_title= (TextView) view.findViewById(R.id.tv_title); TextView tv_description= (TextView) view.findViewById(R.id.tv_description); TextView tv_type= (TextView) view.findViewById(R.id.tv_type); NewsInfo newsInfo=newsInfos.get(position); siv.setImageUrl(newsInfo.getIconPath(),R.drawable.a,R.drawable.ic_launcher); tv_title.setText(newsInfo.getTitle()); tv_description.setText(newsInfo.getDescription()); tv_type.setText(newsInfo.getType()); int type=newsInfo.getType(); switch (type){ case 1: tv_type.setText("comment"+newsInfo.getComment()); break; case 2: tv_type.setTextColor(Color.RED); tv_type.setText("headline"); break; case 3: tv_type.setTextColor(Color.BLUE); tv_type.setText("LIVE"); break; } return view; } public Object getItem(int position){ return null; } public long getItemId(int position){ return 0; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_news=(ListView)findViewById(R.id.lv_news); loading=(LinearLayout)findViewById(R.id.loading); fillData2(); } private void fillData2(){ AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); asyncHttpClient.get(getString(R.string.serverurl), new AsyncHttpResponseHandler() { @Override public void onSuccess(String content) { super.onSuccess(content); byte[] bytes=content.getBytes(); ByteArrayInputStream bs=new ByteArrayInputStream(bytes); newsInfos= NewsInfoService.getNewsInfos(bs); if(newsInfos==null){ Toast.makeText(MainActivity.this,"fail",Toast.LENGTH_LONG).show(); }else{ loading.setVisibility(View.INVISIBLE); lv_news.setAdapter(new NewsAdapter()); } } @Override public void onFailure(Throwable error,String content) { super.onFailure(error,content); Toast.makeText(MainActivity.this, "request fail", Toast.LENGTH_SHORT).show(); } }); }}
(6).配置服务器,先开启服务器并运行start.bat。把要解析的xml文件放入到tomcat的bin文件夹下的web apps的root文件夹下
(7)在清单文件中添加权限
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bz0209.news"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application><uses-permission android:name="android.permission.INTERNET"/></manifest>
阅读全文
0 0
- 简易新闻客户端案例
- 案例-----简易新闻客户端
- 网络编程案例简易新闻客户端
- 网络编程小案例之简易新闻客户端
- 简易新闻客户端
- 简易新闻客户端
- Android简易新闻客户端
- 新闻客户端案例开发
- 新闻客户端案例
- 案例--新闻客户端
- 实现案例新闻客户端
- 新闻客户端案例
- 案例----新闻客户端
- 新闻客户端案例
- 案例-----新闻客户端
- 新闻客户端案例
- 简单新闻客户端案例
- 新闻客户端综合案例
- TCP/IP基础知识
- 命令模式--软件设计模式
- 欢迎使用CSDN-markdown编辑器
- hasNext与next方法
- 那些年我们一起追过的缓存写法(一)
- 简易新闻客户端案例
- @Value值注入及配置文件组件扫描方式
- 被人遗忘的MAX_FILE_SIZE文件上传限制大小参数
- 对IO流的理解
- 百度文件下载代码
- 获取笔记本电脑oem激活密钥
- Android酷炫实用的开源框架(UI框架)
- 随机变量序列的收敛性质分类
- 程序员你伤不起(二)