Android模拟实现网易新闻客户端
作者:Prince_J 发布时间:2022-09-03 18:19:11
标签:Android,新闻,客户端
首先我们先看一下要模拟的界面
我们主要实现的就是ListView解析json文件中的数据,UI布局很简单不做赘述。
这里我们需要一个服务器来实现数据的动态更新, 这里我们用到的是Tomcat8.0。
首先我们把需要解析的json文件放置到Tomcat的webapp文件下的ROOT里面,方便我们解析。
首先我们创建一个JsonParse类用来解析json文件:
package cn.edu.bzu.myapplication.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
import cn.edu.bzu.myapplication.entity.NewsInfo;
/**
* Created by Becauseshy on 2017/5/18.
*/
public class JsonParse {
public static List<NewsInfo> getNewInfo(String json){
Gson gson=new Gson();
Type listType=new TypeToken<List<NewsInfo>>(){
}.getType();
List<NewsInfo> newsInfos=gson.fromJson(json,listType);
return newsInfos;
}
}
创建json文件的实体类:
package cn.edu.bzu.myapplication.entity;
/**
* Created by Becauseshy on 2017/5/17.
*/
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;
}
}
activity_main.xml:
<?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>
item的布局文件:
<?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>
适配器代码:
package cn.edu.bzu.myapplication.adapter;
/**
* Created by Becauseshy on 2017/5/17.
*/
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.loopj.android.image.SmartImageView;
import java.util.List;
import cn.edu.bzu.myapplication.R;
import cn.edu.bzu.myapplication.entity.NewsInfo;
public class NewAdapter extends ArrayAdapter<NewsInfo>{
private int resourceID;
public NewAdapter(Context context, int resource, List<NewsInfo> objects) {
super(context, resource, objects);
resourceID=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NewsInfo fruit=getItem(position);
View view;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceID,null);
viewHolder=new ViewHolder();
viewHolder.siv=(SmartImageView)view.findViewById(R.id.siv_icon);
viewHolder.tv_title=(TextView)view.findViewById(R.id.tv_title);
viewHolder.tv_description=(TextView)view.findViewById(R.id.tv_description);
viewHolder.tv_type=(TextView)view.findViewById(R.id.tv_type);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder= (ViewHolder) view.getTag();
}
viewHolder.siv.setImageUrl(fruit.getIconPath(),R.drawable.a,R.drawable.ic_launcher);
viewHolder.tv_title.setText(fruit.getTitle());
viewHolder.tv_description.setText(fruit.getDescription());
int type=fruit.getType();
switch (type){
case 1:
viewHolder.tv_type.setText("评论:"+fruit.getComment());
viewHolder.tv_type.setTextColor(Color.BLUE);
break;
case 2:
viewHolder.tv_type.setText("专题");
viewHolder.tv_type.setTextColor(Color.BLACK);
break;
case 3:
viewHolder.tv_type.setText("LIVE");
viewHolder.tv_type.setTextColor(Color.RED);
break;
}
return view;
}
class ViewHolder{
SmartImageView siv;
TextView tv_title;
TextView tv_description;
TextView tv_type;
}
}
MainActivity实现代码:
package cn.edu.bzu.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bzu.myapplication.Tools.JsonParse;
import cn.edu.bzu.myapplication.adapter.NewAdapter;
import cn.edu.bzu.myapplication.entity.NewsInfo;
import cn.edu.bzu.myapplication.model.Fruit;
public class MainActivity extends AppCompatActivity {
private ListView Iv_news;
private NewAdapter newAdapter;
private List<NewsInfo> newInfos;
private LinearLayout loading;
private JsonParse jsonParse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Iv_news= (ListView) findViewById(R.id.lv_news);
newAdapter =new NewAdapter(this,R.layout.news_item,newInfos);
loading= (LinearLayout) findViewById(R.id.loading);
prepareData();
}
private void prepareData() {
//fruitList=new ArrayList<>();
//Fruit apple=new Fruit("Apple",R.drawable.apple_pic);
// fruitList.add(apple);
AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
asyncHttpClient.get(getString(R.string.serverurl), new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
try {
String json=new String(bytes,"utf-8");
newInfos=jsonParse.getNewInfo(json);
if(newInfos==null){
Toast.makeText(MainActivity.this,"解析失败",Toast.LENGTH_SHORT).show();
}
else {
loading.setVisibility(View.INVISIBLE);
Iv_news.setAdapter(newAdapter);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) {
Toast.makeText(MainActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
}
});
}
}
在values文件加下的String.xml文件中添加:
<string name="serverurl">http://172.16.26.58:8080/newInfo.xml</string>
最后一定不要忘了添加网络访问权限, 这很重要, 好多同学都犯了这个错误、
这样基本功能就实现了。


猜你喜欢
- StringString类是不可变类,即一旦一个String对象被创建以后,包含在这个对象中的字符序列是不可改变的,直至这个对象被销毁。这个
- 前言工作中是否有这样的场景,多个线程任务,如果所有线程完成到某个阶段,你希望知道所有线程均完成该阶段。当然你使用线程计数可以实现,只是不够优
- 前言在Java中,集合和数组是我们经常会用到的数据结构,需要经常对他们做增、删、改、查、聚合、统计、过滤等操作。相比之下,关系型数据库中也同
- 同类型对象的比较三个维度去比较同一性相等性相似性样例引入想象一下这样的一个场景:小王去图书馆借了一本java核心技术卷1,如图不幸的是小王把
- 一、堆的概念堆的定义:n个元素的序列{k1 , k2 , … , kn}称之为堆,当且仅当满足以下条件时:(1)ki
- 本文实例为大家分享了C#访问共享文件夹或者磁盘的具体代码,供大家参考,具体内容如下SharedTool:using System; &nbs
- 很多程序员都以自己写的代码的行数作为自己程序员阅历的一个标志,如何统计呢,以下是具体内容。小编,已经快学了两年编程了。昨天突发奇想,想统计下
- 一. Base64编码由来为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASC
- POST接口formdata传参模板记录var res = ""; HttpClient _httpClient = n
- 本文实例为大家分享了C#实现剪刀石头布游戏的具体代码,供大家参考,具体内容如下游戏界面如下所示:首先我们必须知道要创建三个类玩家类,电脑类,
- 一.求两直线交点class Point { double x; do
- <profiles> <profile> <
- 前言在单机应用时代,我们对一个共享的对象进行多线程访问的时候,使用java的synchronized关键字或者ReentrantLock类对
- 本文实例为大家分享了Android实现页面短信验证功能的具体代码,供大家参考,具体内容如下目标效果:上一篇博文介绍的是使用SDK中自带的验证
- 作为Android的四大组件之一,ContentProvider作为进程之间静态数据传递的重要手段,其在系统级别的应用中起了重大的作用。毫无
- 在java的JFrame内通过创建匿名对象的方式做登录界面package com.sxt;import java.awt.Container
- 文章目录 简介增量构建自定义inputs和outputs运行时API隐式依赖输入校验自定义缓存方法输入归一化其他使用技巧简介在我们使用的各种
- 一开始我就纳闷了,怎么调试都只是一个光溜溜的界面,右侧的工具栏都没有如图:就一个光秃秃的界面,什么都没有,这就对调试很不方便于是我就试了试各
- 主要介绍springboot项目中配置文件的加密前言为了保证服务器相关信息的保密,一般会采用加密的方式进行对配置文件原文的加密,今天介绍下s
- 前言自 Java 7 以来,java 中的 switch 语句经历了快速发展。因此,在本文中,我们将通过示例讨论 switch 语句从 ja