新闻客户端
来源:互联网 发布:数据产品经理做什么 编辑:程序博客网 时间:2024/05/19 18:39
一、源代码
package cn.edu.bzu.newsclient;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.AsyncHttpResponseHandler;import com.loopj.android.image.SmartImageView;import org.apache.http.Header;import java.io.ByteArrayInputStream;import java.util.List;public class MainActivity extends AppCompatActivity { private ListView lv_news; private LinearLayout loading; private ListnewsInfos; private class NewsAdapter extends BaseAdapter { public int getCount() { return newsInfos.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this, R.layout.news_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.mipmap.ic_launcher, R.mipmap.ic_launcher); tv_title.setText(newsInfo.getTitle()); tv_description.setText(newsInfo.getDescription()); int type = newsInfo.getType(); switch (type) { case 1: tv_type.setText("评论:" + newsInfo.getComment()); break; case 2: tv_type.setTextColor(Color.RED); tv_type.setText("专题"); break; case 3: tv_type.setTextColor(Color.BLUE); tv_type.setText("LIVE"); break; } return view; } @Override public Object getItem(int position) { return null; } @Override 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 bais = new ByteArrayInputStream(bytes); newsInfos = NewsInfoService.getNewsInfos(bais); if (newsInfos == null) { Toast.makeText(MainActivity.this, "解析失败", Toast.LENGTH_SHORT).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, "请求失败", Toast.LENGTH_SHORT).show(); } }); }}package cn.edu.bzu.newsclient;/** * Created by zhangaozhi on 2017/5/24. */public class NewsInfo { private String iconPath; private String title; private String description; private int type; private long comment; 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 String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getType() { return type; } public void setType(int type) { this.type = type; } public long getComment() { return comment; } public void setComment(long comment) { this.comment = comment; }}package cn.edu.bzu.newsclient;import android.util.Xml;import android.widget.Switch;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import static cn.edu.bzu.newsclient.R.drawable.e;/** * Created by zhangaozhi on 2017/5/24. */public class NewsInfoService { private static List getNewsInfos(InputStream is) throws IOException { XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(is, "utf-8"); int type = parser.getEventType(); List newsInfos = null; NewsInfo newsInfo = null; while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_TAG: if ("news".equals(parser.getName())) { newsInfos = new ArrayList (); } 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.setTitle(description); } else if ("type".equals(parser.getName())) { String newsType = parser.nextText(); newsInfo.setTitle(newsType); } else if ("comment".equals(parser.getName())) { String comment = parser.nextText(); newsInfo.setTitle(comment); } break; case XmlPullParser.END_TAG: if ("newsInfo".equals(parser.getName())) { newsInfos.add(newsInfo); newsInfo = null; } break; } type = parser.next(); } return newsInfos; } catch (Exception e) { e.printStackTrace(); return null; } }}
由于条件不允许没有结果图。
阅读全文
0 0
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- Android中将布局文件/View添加至窗口过程分析 ---- 从setContentView()谈起
- 云服务器配置流程
- redis集群搭建
- [系分] 用例图
- NOIP2003(洛谷 P1038 )神经网络 COGS 104
- 新闻客户端
- http概述(http权威指南第一章)
- Poedu_Windows_CreateFile浅析
- Java中Synchronized的用法
- 获取键盘鼠标操作的函数(GetAsyncKeyState ())
- Netty 服务器客户端,发送自定义消息
- web_tomcat的那些事儿1:如何配置tomcat(window)
- 丑数问题及变种小结
- Jupyter notebook入门教程(上)