当前位置: 移动技术网 > IT编程>移动开发>Android > Android网络编程之简易新闻客户端

Android网络编程之简易新闻客户端

2019年07月24日  | 移动技术网IT编程  | 我要评论

日本人竟这样玩学生妹,小夫妻时代迅雷下载,sopopo

一、 通过一个案例“新闻客户端”向大家演示asynchttpclient和smartimageview的综合使用。

运行结果如下:

1、首先我们了解一下相关知识: 

smartimageview的使用

市面上一些常见软件,例如手机qq、天猫、京东商场等,都加载了大量网络上的图片。用android自带的api实现这一功能十分麻烦而且耗时。为此,编程爱好者开发了一个开源项目——smartimageview。

(smartimageview的jar包得下载)

开源项目smartimageview的出现主要是为了 加速从网络上加载图片,它继承自imageview类,支持根据url地址加载图片、支持异步加载图片、支持图片缓存等。

asynchttpclient的使用

在android开发中,发送、处理http请求十分常见,如果每次与服务器进行数据交互都需要去开启一个子线程,这样是非常麻烦的。为了解决这个问题,一些开发者开发出了开源项目——asynchttpclient。


asynchttpclient是对httpclient的 再次包装。asynchttpclient的特点有,发送 异步http 请求、http
请求发生在 在ui线程之外 线程之外、内部采用了 线程池来处理并发请求, ,而且它使用起来比httpclient更加简便。

配置tomcat服务器 

下载并通过startup.bat启动服务器

在webapps/root文件夹下:json文件和images文件夹

在这里我就不介绍gson解析了,在我的下一篇博文中会有解释

二、实现步骤如下 


需要创建如上类

• entity包下创建 包下创建实体类 实体类newsinfo

package cn.edu.bzu.anew.entity; 
 
/** 
 * created by administrator on 2017/5/18. 
 */ 
public class newsinfo { 
 private string icon;//图片路径 
 private string title;//新闻标题 
 private string description;//新闻描述 
 private int type;//新闻类型 
 private long comment;//新闻评论数 
 
 public newsinfo(string icon, string title, string description, int type, long comment) { 
  this.icon = icon; 
  this.title = title; 
  this.description = description; 
  this.type = type; 
  this.comment = comment; 
 } 
 
 public string geticon() { 
  return icon; 
 } 
 
 public void seticon(string icon) { 
  this.icon = icon; 
 } 
 
 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; 
 } 
} 

• tools包下创建 包下创建 工具类 类jsonparse 负责解析json数据

package cn.edu.bzu.anew.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.anew.entity.newsinfo; 
 
/** 
 * created by administrator on 2017/5/18. 
 */ 
 
public class jsonparse { 
 public static list<newsinfo>getnewsinfo(string json){//使用gson库解析json数据 
  gson gson =new gson(); 
  type listtype=new typetoken<list<newsinfo>> (){//创建一个typetoken的匿名子类对象,并调用对象得gettype()方法 
  }.gettype(); 
  list<newsinfo>newsinfos=gson.fromjson(json,listtype);//把获取到的信息集合存到newsinfos中 
  return newsinfos; 
 } 
} 

 adapter 包下创建newsadapter类

package cn.edu.bzu.anew.adapter; 
import android.content.context; 
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.anew.r; 
import cn.edu.bzu.anew.entity.newsinfo; 
public class newsadapter extends arrayadapter<newsinfo>{ 
 public newsadapter(context context, list<newsinfo> objects) { 
  super(context, r.layout.news_item, objects); 
 
 } 
 
 @nonnull 
 @override 
 public view getview(int position, view convertview, viewgroup parent) { 
  newsinfo newsinfo= getitem(position);//传递position,获取当前位置对应的newsinfo新闻信息 
  view view=null; 
  viewholder viewholder; 
  if(convertview==null){ //判断convertview中是否加载了布局,有没有缓存。为空说明没有缓存 
   view=layoutinflater.from(getcontext()).inflate(r.layout.news_item,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(newsinfo.geticon());//传递图片地址 
  viewholder.tv_title.settext(newsinfo.gettitle());//传递题目 
  viewholder.tv_description.settext(newsinfo.getdescription()); 
  viewholder.tv_type.settext(newsinfo.gettype()+""); 
  return view; 
 } 
 class viewholder{//添加类,封装需要查找的控件 
  textview tv_title; 
  textview tv_description; 
  textview tv_type; 
  smartimageview siv; 
 
 } 
} 

 界面逻辑代码的设计与实现(mainactivity)

package cn.edu.bzu.anew; 
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.list; 
import cn.edu.bzu.anew.tools.jsonparse; 
import cn.edu.bzu.anew.adapter.newsadapter; 
import cn.edu.bzu.anew.entity.newsinfo; 
public class mainactivity extends appcompatactivity { 
 private listview lvnews; 
 private list<newsinfo> newsinfos; 
 private linearlayout loading; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
  lvnews=(listview)findviewbyid(r.id.lv_news); 
  loading=(linearlayout)findviewbyid(r.id.loading); 
  filldata(); 
 
 } 
 private void filldata(){ 
  asynchttpclient client =new asynchttpclient(); 
 
  client.get("http://10.61.28.176:8080/newsinfo.json",new asynchttpresponsehandler(){ 
   @override 
   public void onsuccess(int i, org.apache.http.header[] headers, byte[] bytes) { 
    try{ 
     string json=new string(bytes,"utf-8"); 
     newsinfos= jsonparse.getnewsinfo(json); 
     if(newsinfos==null){ 
      toast.maketext(mainactivity.this,"解析失败", toast.length_long).show(); 
     }else{ 
      loading .setvisibility(view.invisible); 
      lvnews.setadapter(new newsadapter(mainactivity.this,newsinfos)); 
     } 
    } catch (unsupportedencodingexception e) { 
     e.printstacktrace(); 
    } 
   } 
 
   @override 
   public void onfailure(int i, org.apache.http.header[] headers, byte[] bytes, throwable throwable) { 
 
   } 
  } ); 
 
 
 } 
} 

在androidmanifest.xml添加访问权限在</application>外<uses-permission android:name="android.permission.internet"></uses-permission>

最后项目就完成了

有以下注意事项需要我们注意:
(1)我们在自己的电脑上运行项目时要用自己的ip地址 json文件中也是如此

(2)在这里我们需要添加三个jar包,记得as library(在projects---app---libs)

(3)

 

如果出现以上问题 ,图片加载失误 当地址都正确  ,那就是你没有添加网络加载图片还有就是把图片后缀jpg改为png
viewholder.siv.setimageurl(newsinfo.geticon());

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网