展示效果

首先添加依赖

compile 'com.squareup.retrofit2:retrofit:2.0.1'compile 'com.squareup.retrofit2:converter-gson:2.0.1'compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'compile 'io.reactivex:rxandroid:1.1.0'compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'compile 'com.android.support:mediarouter-v7:25.0.0'compile 'com.android.support:appcompat-v7:25.0.0'compile 'com.android.support:recyclerview-v7:25.0.0'compile files('libs/universal-image-loader-1.9.3.jar')compile 'com.facebook.fresco:fresco:0.12.0' // 支持 GIF 动图,需要添加compile 'com.facebook.fresco:animated-gif:0.12.0'

添加权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

主Activity(MainActivity)

public class MainActivity extends AppCompatActivity {private RecyclerView mRecyclerView;private List<News.DataBean> mDatas;private HomeAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mDatas=new ArrayList<News.DataBean>();mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));getnew();}private void getnew(){Retrofit retrofit = new Retrofit.Builder().baseUrl(API.HOME_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();//通过动态代理得到网络接口对象ApiService apiService = retrofit.create(ApiService.class);Observable<News> news = apiService.getNews();news.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<News>() {@Overridepublic void onCompleted() {Toast.makeText(MainActivity.this, "onCompleted", Toast.LENGTH_SHORT).show();}@Overridepublic void onError(Throwable e) {Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();}@Overridepublic void onNext(News news) {mDatas = news.getData();mRecyclerView.setAdapter(mAdapter = new HomeAdapter(MainActivity.this,mDatas));}});}
}

App

public class App extends Application {@Overridepublic void onCreate() {super.onCreate();Fresco.initialize(this);}
}

创建一个接口类(ApiService )

public interface ApiService {@GET("type/1/p/1")Observable<News> getNews();}

接口类(API)

public class API {public static  final  String BASE_PATH = "http://service.meiyinkeqiu.com";public static final String BASE_URL = "https://api.github.com/";public static final String HOME_URL="http://api.expoon.com/AppNews/getNewsList/";
}

外部适配器(HomeAdapter)

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {Context mcontext;List<News.DataBean> mlist;public HomeAdapter(Context mcontext, List<News.DataBean> mlist) {this.mcontext = mcontext;this.mlist = mlist;}@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_home, parent,false));return holder;}@Overridepublic void onBindViewHolder(MyViewHolder holder, int position){holder.tv.setText(mlist.get(position).getNews_title());Uri uri = Uri.parse(mlist.get(position).getPic_url());holder.img.setImageURI(uri);DraweeController controller = Fresco.newDraweeControllerBuilder()//重试之后要加载的图片URI地址.setUri(uri)//设置点击重试是否开启.setTapToRetryEnabled(true)//动画播放.setAutoPlayAnimations(true)//设置旧的Controller.setOldController(holder.img.getController())//构建.build();//设置DraweeControllerholder.img.setController(controller);}@Overridepublic int getItemCount(){return mlist.size();}class MyViewHolder extends RecyclerView.ViewHolder{SimpleDraweeView img;TextView tv;public MyViewHolder(View view){super(view);img=(SimpleDraweeView) view.findViewById(R.id.img);tv = (TextView) view.findViewById(R.id.text1);}}
}

解析类(News)

public class News {/*** status : 1* info : 获取内容成功* data : [{"news_id":"13811","news_title":"深港澳台千里连线,嘉年华会今夏入川","news_summary":"6月17\u201420日,\u201c2016成都深港澳台嘉年华会\u201d(简称嘉年华会)将在成都世纪城国际会展中心举办。其主办方励展华博借力旗","pic_url":"http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg"},{"news_id":"13810","news_title":"第14届温州国际汽车展4月举行 设9大主题展馆","news_summary":"来自前不久举行的温州国际汽车展览会第一次新闻发布会的消息, 2016第14届温州国际汽车展览会定于4月7-10日在温州国","pic_url":"http://f.expoon.com/sub/news/2016/01/21/580828_230x162_0.jpg"},{"news_id":"13808","news_title":"第十二届中国(南安)国际水暖泵阀交易会 四大亮点","news_summary":"第十二届中国(南安)国际水暖泵阀交易会将于2月10日至12日(即农历正月初三至初五)在成功国际会展中心拉开帷幕。","pic_url":"http://f.expoon.com/sub/news/2016/01/21/745921_230x162_0.jpg"},{"news_id":"13805","news_title":"2016上海灯光音响展 商机无限,一触即发","news_summary":"2016上海国际专业灯光音响展即日起全面启动,海内外高端演艺设备商贸平台,商机无限,一触即发。6大洲,80个国家,25,","pic_url":"http://f.expoon.com/sub/news/2016/01/21/158040_230x162_0.jpg"},{"news_id":"13804","news_title":"第四届南京国际佛事展5月举行","news_summary":"2016年,\u201c第四届南京国际佛事文化用品展览会\u201d将于5月26-29日在南京国际展览中心举办。","pic_url":"http://f.expoon.com/sub/news/2016/01/21/865222_230x162_0.jpg"},{"news_id":"13802","news_title":"上海国际牛仔服装博览会 拓展国际贸易大市场","news_summary":"2016年第三届上海国际牛仔服装博览会将于4月19-21日再次璀璨再现上海世博展览馆,共同探讨牛仔流行趋势,诠释牛仔文化","pic_url":"http://f.expoon.com/sub/news/2016/01/20/370858_230x162_0.jpg"},{"news_id":"13800","news_title":"第三届兰州年货会在甘肃国际会展中心本月19日开幕","news_summary":"由中国商业联合会、甘肃省商业联合会、兰州市商务局主办,甘肃省酒类商品管理局、兰州市城关区商务局、第十四届西安年货会组委会","pic_url":"http://f.expoon.com/sub/news/2016/01/20/868385_230x162_0.jpg"},{"news_id":"13799","news_title":"首届移动拍卖艺术博览会启动","news_summary":"首届移动拍卖博览会已于2016年1月全面启动,由大咖拍卖主办,联合全国艺术机构共同打造拍卖艺术博览会主会场,近百场拍卖专","pic_url":"http://f.expoon.com/sub/news/2016/01/20/768695_230x162_0.jpg"},{"news_id":"13798","news_title":"武汉金融理财投资博览会将在5月举办","news_summary":"由武汉市贸促会、上海《理财周刊》社、湖北好博塔苏斯展览有限公司等单位联合发起的\u201c2016武汉金融理财投资博览会\u201d,将在武","pic_url":"http://f.expoon.com/sub/news/2016/01/20/512947_230x162_0.jpg"},{"news_id":"13796","news_title":"第三届中国微商博览会 3月底济南举办","news_summary":"2015年,沸点天下开创了微商行业第一个展会\u2014\u2014中国微商博览会,并于2015年成功举行两届,让微商展会从无到有,并且起了","pic_url":"http://f.expoon.com/sub/news/2016/01/20/348021_230x162_0.jpg"},{"news_id":"13793","news_title":"2016中国西部国际丝绸博览会","news_summary":"\u201c2016年中国西部国际丝绸博览会\u201d最新确定于2016年5月11日至15日在南充举行。据悉,\u201c丝博会\u201d的会徽、会标及宣传","pic_url":"http://f.expoon.com/sub/news/2016/01/19/113912_230x162_0.jpg"},{"news_id":"13792","news_title":"中国针棉织品交易会开拓\u201c西部市场\u201d","news_summary":"由国家商务部重点支持、中国纺织品商业协会主办的第98届中国针棉织品交易会将于3月15日~17日绽放成都。作为中国国内针棉","pic_url":"http://f.expoon.com/sub/news/2016/01/19/650175_230x162_0.jpg"},{"news_id":"13791","news_title":"乐山市第二十届房地产展示交易会开幕","news_summary":"美丽乐山,生态宜居。今日,乐山市第二十届房地产展示交易会在该市中心城区乐山广场开幕,展会将持续到1月24日。","pic_url":"http://f.expoon.com/sub/news/2016/01/19/321787_230x162_0.jpg"},{"news_id":"13790","news_title":"2016华中屋面与建筑防水技术展3月即将开幕","news_summary":"由湖北省建筑防水协会联合湖南、河南、江西、安徽五省建筑防水协会主办\u201c2016第二届华中屋面与建筑防水技术展览会\u201d将于20","pic_url":"http://f.expoon.com/sub/news/2016/01/19/376254_230x162_0.jpg"},{"news_id":"13789","news_title":"2016海南国际旅游贸易博览会召开新闻发布会","news_summary":"近日,三亚旅游官方网从海南省\u201c首届海博会\u201d新闻发布会上获悉,海南省\u201c首届海博会\u201d将于2016年3月26日至4月1日在三亚","pic_url":"http://f.expoon.com/sub/news/2016/01/19/958046_230x162_0.jpg"},{"news_id":"13788","news_title":"2016阿里巴巴·贵州年货节展销会开幕","news_summary":"\u201c2016阿里巴巴·贵州年货节\u201d的展销会及迎春庙会昨日启动。150多家餐饮商参与的美食节、近千个品种组成的年货展销会等,","pic_url":"http://f.expoon.com/sub/news/2016/01/19/371688_230x162_0.jpg"},{"news_id":"13787","news_title":"第二届中国盆栽花卉交易会\u200b 本月28日开幕","news_summary":"据广州市政府获悉,经中国花卉协会和广州市政府批准,第二届中国盆栽花卉交易会将于本月28日至31日在广州花卉博览园举行。届","pic_url":"http://f.expoon.com/sub/news/2016/01/18/687647_230x162_0.jpg"},{"news_id":"13786","news_title":"李益:视野、品质、融合是展览工程国际化的必由路径","news_summary":"\u201c视野、品质、融合是中国展览工程走向国际化的必由路径。\u201d北京逸格天骄国际展览有限公司副总经理李益日前在第二十二届国际(常","pic_url":"http://f.expoon.com/sub/news/2016/01/18/343556_230x162_0.jpg"},{"news_id":"13785","news_title":"第八届中国国际集成住宅产业博览会将于5月在广州举办","news_summary":"2016年1月14日,第八届中国(广州)国际集成住宅产业博览会暨2016亚太建筑科技论坛\u2014\u2014新闻发布会在广州馆隆重召开。","pic_url":"http://f.expoon.com/sub/news/2016/01/18/581830_230x162_0.jpg"},{"news_id":"13784","news_title":"丝绸之路敦煌国际文化博览会筹备工作进展顺利","news_summary":"近日,丝绸之路(敦煌)国际文化博览会组委会第二次会议在兰召开。会议研究讨论了省直厅局一对一服务保障沿线省区市方案、文博会","pic_url":"http://f.expoon.com/sub/news/2016/01/18/656693_230x162_0.jpg"}]*/private int status;private String info;private List<DataBean> data;public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public String getInfo() {return info;}public void setInfo(String info) {this.info = info;}public List<DataBean> getData() {return data;}public void setData(List<DataBean> data) {this.data = data;}public static class DataBean {/*** news_id : 13811* news_title : 深港澳台千里连线,嘉年华会今夏入川* news_summary : 6月17—20日,“2016成都深港澳台嘉年华会”(简称嘉年华会)将在成都世纪城国际会展中心举办。其主办方励展华博借力旗* pic_url : http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg*/private String news_id;private String news_title;private String news_summary;private String pic_url;public String getNews_id() {return news_id;}public void setNews_id(String news_id) {this.news_id = news_id;}public String getNews_title() {return news_title;}public void setNews_title(String news_title) {this.news_title = news_title;}public String getNews_summary() {return news_summary;}public void setNews_summary(String news_summary) {this.news_summary = news_summary;}public String getPic_url() {return pic_url;}public void setPic_url(String pic_url) {this.pic_url = pic_url;}}
}

主布局(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="test.bwie.com.rxjavalianxi1.MainActivity"><android.support.v7.widget.RecyclerView
        android:id="@+id/id_recyclerview"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></RelativeLayout>

item布局(item_home.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="wrap_content"xmlns:fresco="http://schemas.android.com/apk/res-auto"><com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/img"android:layout_margin="5dp"android:layout_width="100dp"android:layout_height="100dp"fresco:actualImageScaleType="focusCrop"fresco:placeholderImage="@mipmap/placeholder_image"fresco:placeholderImageScaleType="focusCrop"fresco:progressBarImage="@mipmap/progress_image"fresco:progressBarImageScaleType="focusCrop"fresco:progressBarAutoRotateInterval="5000"fresco:failureImage="@mipmap/error"fresco:failureImageScaleType="focusCrop"fresco:retryImage="@mipmap/retry"fresco:retryImageScaleType="focusCrop"fresco:fadeDuration="5000"fresco:backgroundImage="@android:color/holo_orange_light"fresco:roundAsCircle="true"fresco:roundedCornerRadius="30dp"fresco:roundTopLeft="true"fresco:roundTopRight="true"fresco:roundBottomLeft="true"fresco:roundBottomRight="true"fresco:roundingBorderWidth="5dp"fresco:roundingBorderColor="#008dd7"/><TextView
        android:id="@+id/text1"android:layout_toRightOf="@id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="Hello World!"android:textColor="@color/colorAccent"android:textSize="20dp"/>
</RelativeLayout>

点击这里下载布局里的图片资源 密码:8kqk

谢谢观看,小编祝大家生活愉快!(多多关注小编,会有非常之多精彩分享哦!)

Android RxJava与Retrofit与RecyclerView与Fresco结合网络请求相关推荐

  1. java 轮询请求接口_Android RxJava 实际应用讲解:(无条件)网络请求轮询

    前言 Rxjava,由于其基于事件流的链式调用.逻辑简洁 & 使用简单的特点,深受各大 Android开发者的欢迎. Github截图 RxJava如此受欢迎的原因,在于其提供了丰富 & ...

  2. Android秀翻天的操作——使用协程进行网络请求

    文章目录 一.前言 二.Kotlin协程 三.协程和Retrofit 四.预备工作 1.准备好Retrofit 2.准备好协程 五.使用 六.小结 本文已独家授权 郭霖 ( guolin_blog) ...

  3. 【Android开发】计算机网络基础知识点,如何完成网络请求过程?

    (一)计算机网络基础知识:从一次完整的网络请求过程分析 (1)域名解析 1.1)域名与ip地址 (1)ip地址:ip地址是一个32位(4字节)的二进制数(IPV4),常见格式为:192.168.1.1 ...

  4. android studio 接口方法,Android Studio第三十期 - 介绍几种网络请求方式写法

    框架git地址:附(爱一个人就要爱他(loader)的全部~) 请求方式:POST 请求示例{ "verify_info":{ "app":{ "ap ...

  5. 图片加载用Fresco,网络请求用OKhttp+Retrofit实现

    --------------清单文件------------------- AndroidManifest <manifest xmlns:android="http://schema ...

  6. android 服务器201,【报Bug】h5+app 网络请求 状态码返回201会报错200不会

    Android 系统 h5+app request接口 GET 请求 服务器返回状态码201会出错 (h5 小程序不会出错) {"data":"Attempt to in ...

  7. Rxjava、Retrofit返回json数据解析异常处理

    每个App都避免不了要进行网络请求,从最开始的用谷歌封装的volley到再到android-async-http再到OKHttpUtils再到现在的Retrofit和RxJava,从我自己用后的体验来 ...

  8. Android RxJava操作符的学习---功能性操作符--(有条件)网络请求轮询(结合Retrofit)

    1. 需求场景 2. 功能说明 采用Get方法对 金山词霸API 按规定时间重复发送网络请求,从而模拟 轮询 需求实现 停止轮询的条件 = 当轮询到第4次时 采用 Gson 进行数据解析 3. 具体实 ...

  9. Android RxJava操作符的学习---功能性操作符--网络请求出错重连(结合Retrofit)

    1. 需求场景 2. 功能说明 功能需求说明 功能逻辑  实例说明 在本例子中:采用Get方法对 金山词霸API 发送网络请求 通过 断开网络连接 模拟 网络异常错误(恢复网络即可成功发送请求) 限制 ...

最新文章

  1. 调整图像- 自动对比度、自动色阶算法
  2. ubuntu常用小工具
  3. leetcode 4. 寻找两个有序数组的中位数,c语言
  4. 从华为“鸿蒙”备胎看IT项目建设
  5. Java代码中方法的特殊修饰符/修饰词/关键字
  6. spring IOC基本配置(xml配置和注解配置)
  7. python propresql mysql_python数据库操作mysql:pymysql、sqlalchemy常见用法详解
  8. Zookeeper集群详解
  9. 在C#中??和?分别是什么意思?
  10. Shell_Shell调用SQLPlus简介(案例)
  11. pytorch visdom可视化工具学习—1—详细使用-3-Generic Plots和Others
  12. VBM计算操作过程记录
  13. 微信消息实现自动推送--方式一 成功啦 进来学
  14. 博客在六个方面降低网络营销费用
  15. 可能被你误解的 DRY 原则
  16. 钝化程序模式SOA架构BPM
  17. android跳转到微信小程序
  18. 用计算机弹童年,你的童年电脑记忆里有它们吗?瑞星小狮子、Office曲别针、三维弹球……...
  19. 大数据精准投放平台_大数据精准营销+全媒体精准投放+信息流广告=真实有效得客户...
  20. 西北乱跑娃 --- opencv泛洪填充学习笔记

热门文章

  1. 2,未来十年,资产增加
  2. 英特尔和amd学计算机,笔记本处理器intel和amd哪个好_有什么区别|性能对比-太平洋电脑网...
  3. 51 Nod1113 矩阵快速幂
  4. 4.Matplotlib多子图,文字和注释以及自定义坐标轴
  5. 贝叶斯网络(Bayesian Network)
  6. 机器学习——特征工程
  7. python3中 operator模块用法介绍
  8. 禅意茶艺茶道茶文化PPT模板
  9. C#,JAVA人民币小写转大写
  10. SKNet: Selective Kernel Networks