新闻客户端
来源:互联网 发布:淘宝的工具吧在哪里 编辑:程序博客网 时间:2024/05/17 02:52
1. 内容摘要
Gson解析json数据,不熟悉Gson解析的同学,请看使用Gson解析json数据
SmartImageView的使用– https://github.com/loopj/android-smart-image-view
android轻量级网络请求框架MyHttputils http://blog.csdn.net/qq137722697
2.新闻数据
[ { "icon": "http://10.51.10.182:8080/images/a.jpg", "title": "科技温暖世界", "content": "进入一个更有爱的领域", "type": "1", "comment": "69" }, { "icon": "http://10.51.10.182:8080/images/b.jpg", "title": "《神武》", "content": "新美术资源盘点,视觉新体验", "type": "2", "comment": "35" }, { "icon": "http://10.51.10.182:8080/images/c.jpg", "title": "南北车正式公布合并", "content": "南北车将于今日正式公布合并", "type": "3", "comment": "2" }, { "icon": "http://10.51.10.182:8080/images/d.jpg", "title": "萌呆了!汪星人抱玩偶酣睡", "content": "汪星人抱玩偶酣睡,萌翻网友", "type": "1", "comment": "25" }, { "icon": "http://10.51.10.182:8080/images/e.jpg", "title": "风力发电进校园", "content": "风力发电普进校园", "type": "2", "comment": "26" }, { "icon": "http://10.51.10.182:8080/images/f.jpg", "title": "地球一小时", "content": "地球熄灯一小时", "type": "1", "comment": "23" }, { "icon": "http://10.51.10.182:8080/images/g.jpg", "title": "最美公路", "content": "最美公路,难以想象", "type": "1", "comment": "23" }]
对应实体代码
public class NewInfo { 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; }}
3.布局文件
main
<?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>news
<?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>
4.代码实现
public class MainActivity extends Activity { private ListView lv_nenw; private LinearLayout loading; private List<NewInfo> newInfos; private class NewsAdapter extends BaseAdapter { public int getCount() { return newInfos.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View view1 = View.inflate(MainActivity.this, R.layout.news_item, null); SmartImageView siv = (SmartImageView) view1.findViewById(R.id.siv_icon); TextView tv_title = (TextView) view1.findViewById(R.id.tv_title); TextView tv_description = (TextView) view1.findViewById(R.id.tv_description); TextView tv_type = (TextView) view1.findViewById(R.id.tv_type); NewInfo newInfo = newInfos.get(i); siv.setImageUrl(newInfo.getIconPath(), R.drawable.a, R.drawable.b); tv_title.setText(newInfo.getTitle()); tv_description.setText(newInfo.getDescription()); int type = newInfo.getType(); switch (type) { case 1: tv_type.setText("评论:" + newInfo.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 view1; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_nenw= (ListView) findViewById(R.id.lv_news); loading= (LinearLayout) findViewById(R.id.loading); filldata2(); } private void filldata2() { MyHttpUtils.build()//构建myhttputils .url("http://10.51.10.182:8080/NewsInfo.json")//请求的url .onExecute(new StringCallBack() {//开始执行,并有一个回调(异步的哦---->直接可以更新ui) @Override public void onSucceed(String result) {//请求成功之后会调用这个方法 //成功之后的拿到结果result进行处理 newInfos=JsonParse.getNewsInfo(result); if (newInfos==null){ Toast.makeText(MainActivity.this,"解析失败",Toast.LENGTH_SHORT).show(); }else { loading.setVisibility(View.INVISIBLE);; lv_nenw.setAdapter(new NewsAdapter()); } } @Override public void onFailed(Throwable throwable) {//请求失败的时候会调用这个方法 //失败之后的拿到错误结果throwable进行处理 Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_LONG).show(); } }); } /*private void filldata2(){ AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); asyncHttpClient.get("http//gyq-pc:8080/NewsInfo.json", new AsyncHttpResponseHandler() { @Override public void onSuccess(int i, Header[] headers, byte[] bytes) { try { String json=new String (bytes,"utf-8"); newInfos=JsonParse.getNewsInfo(json); if (newInfos==null){ Toast.makeText(MainActivity.this,"解析失败",Toast.LENGTH_SHORT).show(); }else { loading.setVisibility(View.INVISIBLE);; lv_nenw.setAdapter(new NewsAdapter()); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } @Override public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_LONG).show(); } }); }*/}
JsonParse实现
public class JsonParse { public static List<NewInfo> getNewsInfo(String json){ Gson gson =new Gson(); Type listType=new TypeToken<List<NewInfo>>(){} .getType(); List<NewInfo> newinfos=gson.fromJson(json,listType); return newinfos; }}
具体实现过程如上
在实现过程中会遇到导入json包,tomcat配置服务器等问题
过程需要添加网络请求权限
<uses-permission android:name="android.permission.INTERNET" />
倒包过程如下
阅读全文
0 0
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 新闻客户端
- 找到一本不错的Linux电子书,附《Linux就该这么学》章节目录。
- shape的属性
- Redis 数据类型之Hash类型
- Ubuntu 16.04安装Hadoop 2.7.3
- 自动化运维工具之fabric
- 新闻客户端
- 江苏科技大学“FlySpiders”本创小组组员学习记录----王澳刚
- 生成连连看地图(难度:1颗星)
- gdb使用
- 互联网加密与解密
- HCI 基础知识之一
- IOS 版 Opera 已不再更新
- 04-树6 Complete Binary Search Tree (30分)
- Java基础