RecyclerView+Retrofit+OkHttp+RxJava

*图片初始化public class FrescoClass extends Application{    @Override    public void onCreate() {        super.onCreate();        Fresco.initialize(this);    }}

*主页面
public class MainActivity extends AppCompatActivity {

private RecyclerView mRv;

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        Flowable<UserBean> add = RetrofitHelper.getShopAPI().add();        //使用Rxjava异步        add.subscribeOn(Schedulers.io())//被观察者执行线程                .observeOn(AndroidSchedulers.mainThread())//观察者线程                .subscribe(new Consumer<UserBean>() {                    @Override                    public void accept(UserBean userBean) throws Exception {                        List<UserBean.NewslistBean> newslist = userBean.getNewslist();                        mRv.setLayoutManager(new LinearLayoutManager(MainActivity.this));                        MyAdapter myAdapter = new MyAdapter(newslist,MainActivity.this);                        mRv.setAdapter(myAdapter);                       // Toast.makeText(MainActivity.this,newslist.get(1).getCtime(),Toast.LENGTH_LONG).show();                    }                });

}

private void initView() {        mRv = (RecyclerView) findViewById(R.id.rv);    }}

*适配器public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{    private List<UserBean.NewslistBean> list;    private Context context;

public MyAdapter(List<UserBean.NewslistBean> list, Context context) {        this.list = list;        this.context = context;    }

@Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = LayoutInflater.from(context).inflate(R.layout.item,parent,false);        return new MyViewHolder(view);    }

@Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {        MyViewHolder my = (MyViewHolder) holder;        my.tv.setText(list.get(position).getTitle());        Uri uri = Uri.parse(list.get(position).getPicUrl());        my.img.setImageURI(uri);    }

@Override    public int getItemCount() {        return list.size();    }    class MyViewHolder extends RecyclerView.ViewHolder{

private final SimpleDraweeView img;        private final TextView tv;

public MyViewHolder(View itemView) {            super(itemView);            img = itemView.findViewById(R.id.img);           tv = itemView.findViewById(R.id.tv);        }    }}
*网络接口
public class net {    public static final String A = "http://api.tianapi.com/";    public static final String B = "nba/?key=71e58b5b2f930eaf1f937407acde08fe&num=10";}

* 封装的网络请求的类

public class RetrofitHelper {

private static OkHttpClient okHttpClient;

private static ServiceApi serviceApi;

static {        initOkHttpClient();    }

/**     * 初始化 OkHttpClient     */    private static void initOkHttpClient() {

if (okHttpClient == null) {            synchronized (RetrofitHelper.class) {                if (okHttpClient == null) {                    okHttpClient = new OkHttpClient.Builder()                            .build();                }            }        }

}

/**     * 定义一个泛型方法     *     * @param clazz     * @param url     * @param <T>     * @return     */    public static <T> T createAPI(Class<T> clazz, String url) {        Retrofit retrofit = new Retrofit.Builder()                .baseUrl(url)                .client(okHttpClient)                .addConverterFactory(GsonConverterFactory.create())                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                .build();        return retrofit.create(clazz);    }

public static ServiceApi getShopAPI() {        if (serviceApi == null) {            synchronized (RetrofitHelper.class) {                if (serviceApi == null) {                    serviceApi = createAPI(ServiceApi.class, net.A);                }            }        }        return serviceApi;    }

}

*接口public interface ServiceApi {    @GET(net.B)    Flowable<UserBean> add();}*封装bean类
public class UserBean {

/**     * code : 200     * msg : success     * newslist : [{"ctime":"2016-12-04 13:00","title":"格林:理解科尔吸食大麻 不过我从没吸过","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161204/1-161204120131.jpg","url":"http://www.51tyw.com/nba/2421.html"},{"ctime":"2016-12-04 00:00","title":"三分纪录延续!火箭队连续19场比赛命中10+三分球","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203233J3.jpg","url":"http://www.51tyw.com/nba/2417.html"},{"ctime":"2016-12-04 00:00","title":"詹姆斯谈三连败:是时候紧起来了 必须打得男人点","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203234010.jpg","url":"http://www.51tyw.com/nba/2418.html"},{"ctime":"2016-12-03 12:00","title":"骑士输赢都靠三分?那还要詹姆斯做什么?","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161203/1-161203104344.jpg","url":"http://www.51tyw.com/nba/2407.html"},{"ctime":"2016-12-03 00:00","title":"公牛vs骑士直播看点:詹伟兄弟对决","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202221205.jpg","url":"http://www.51tyw.com/nba/2395.html"},{"ctime":"2016-12-02 22:00","title":"火箭和勇士联手创NBA三分纪录","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202214212.jpg","url":"http://www.51tyw.com/nba/2392.html"},{"ctime":"2016-12-02 22:00","title":"巴克利:勇士打得像女式篮球,太软了!","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-161202215032.jpg","url":"http://www.51tyw.com/nba/2394.html"},{"ctime":"2016-12-02 20:00","title":"詹姆斯完成月最佳球员4连霸的壮举!","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161202/1-1612021PI0.jpg","url":"http://www.51tyw.com/nba/2388.html"},{"ctime":"2016-12-01 00:00","title":"威少再下三双战书!詹皇会不会先认怂?","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161128/1-16112Q13251.jpg","url":"http://www.51tyw.com/nba/2374.html"},{"ctime":"2016-12-01 00:00","title":"骑士这样的防守想夺冠?也许卫冕只是空谈!","description":"NBA新闻","picUrl":"http://www.51tyw.com/uploads/allimg/161130/1-161130222229.jpg","url":"http://www.51tyw.com/nba/2375.html"}]     */

private int code;    private String msg;    private List<NewslistBean> newslist;

public int getCode() {        return code;    }

public void setCode(int code) {        this.code = code;    }

public String getMsg() {        return msg;    }

public void setMsg(String msg) {        this.msg = msg;    }

public List<NewslistBean> getNewslist() {        return newslist;    }

public void setNewslist(List<NewslistBean> newslist) {        this.newslist = newslist;    }

public static class NewslistBean {        /**         * ctime : 2016-12-04 13:00         * title : 格林:理解科尔吸食大麻 不过我从没吸过         * description : NBA新闻         * picUrl : http://www.51tyw.com/uploads/allimg/161204/1-161204120131.jpg         * url : http://www.51tyw.com/nba/2421.html         */

private String ctime;        private String title;        private String description;        private String picUrl;        private String url;

public String getCtime() {            return ctime;        }

public void setCtime(String ctime) {            this.ctime = ctime;        }

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 String getPicUrl() {            return picUrl;        }

public void setPicUrl(String picUrl) {            this.picUrl = picUrl;        }

public String getUrl() {            return url;        }

public void setUrl(String url) {            this.url = url;        }    }}
 
 

				
时间: 2024-10-16 06:46:58

RecyclerView+Retrofit+OkHttp+RxJava的相关文章

Mvp Retrofit Okhttp Rxjava

MainActivity package com.bwie.shexinhai; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.bwie.shexinhai.Adapter

Rxjava+ReTrofit+okHttp深入浅出-终极封装

Rxjava+ReTrofit+okHttp深入浅出-终极封装 背景: 学习Rxjava和retrofit已经很长时间了,功能确实很强大,但是使用起来还是有点复杂,代码的重复性太高,所以决定把基于retrofit和rxjava的处理统一封装起来,实现的功能: 1.Retrofit+Rxjava+okhttp基本使用方法 2.统一处理请求数据格式 3.统一的ProgressDialog和回调Subscriber处理 4.取消http请求 5.预处理http请求 5.返回数据的统一判断 效果: 封装

Rxjava+ReTrofit+okHttp深入浅出-终极封装四(多文件下载之断点续传)

Rxjava+ReTrofit+okHttp深入浅出-终极封装四(多文件下载之断点续传) 背景 断点续传下载一直是移动开发中必不可少的一项重要的技术,同样的Rxjava和Retrofit的结合让这个技术解决起来更加的灵活,我们完全可以封装一个适合自的下载框架,简单而且安全! 效果 实现 下载和之前的http请求可以相互独立,所以我们单独给download建立一个工程moudel处理 1.创建service接口 和以前一样,先写接口 注意:Streaming是判断是否写入内存的标示,如果小文件可以

学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐

转载请标明出处: http://blog.csdn.net/iamzgx/article/details/51607387 概括 在上一篇博客android网络框架OkHttp之get请求(源码初识) 讲解了OkHttp的简单使用和OkHttp源码的分析,主要讲解的还是理论上的知识,还是没有去实践下,那么这篇博客里面就来实践下.在上一篇博客里面说到了OkHttp类似HttpUrlConnection.按这样说的话,我们在项目中肯定还是要封装一层.如果嫌封装麻烦的话,也可以拿来主义,比如使用鸿洋大

Android探索之基于okHttp打造自己的网络请求&lt;Retrofit+Okhttp&gt;(五)

前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少收有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下Retrofit+Okhttp的搭配使用. Retrofit介绍: Retrofit和okHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比方说通过注解来配置请求参数,通过工厂来生成CallAdap

Retrofit结合RxJava使用说明

PS:年后一直没有更新博客,实在是太忙了.今天周六也是终于抽出了时间,就决定写一篇Retrofit结合RxJava使用说明.刚好现在写的的项目中也有用到,趁热打铁简单说说.最近也不知道博客写什么内容,大家有什么想了解的内容,也可以评论告诉我,我尽力解答. 如果对Retrofit不了解可以看我之前的一篇博客:Retrofit 2.0使用 ,这次的内容也是在此基础上的.关于RxJava可以参考:学习RxJava(资料汇总),这里我就不多说了. 1.准备工作 我们需要添加相应的依赖: compile

Retrofit+OKHttp 教你怎么持久化管理Cookie

绪论 最近小编有点忙啊,项目比较紧,所以一直在忙活项目,继之前的自定义组件之后就没再写博客了,如果你没看到之前的自定义组件你可以看一下: Android自定义下拉刷新动画–仿百度外卖下拉刷新 Android自定义组合控件-教你如何自定义下拉刷新和左滑删除 效果还行,源码也已经传到我的Github上了. 那么今天小编来给大家分享点什么呢?对,就是它:Retrofit,话说Retrofit最近真的很火啊,Retrofit+OKHttp现在似乎已经成为了Android网络请求框架的主流框架了吧,小编之

Retrofit/OkHttp API接口加固技术实践(下)

作者/Tamic http://blog.csdn.net/sk719887916/article/details/65448628 上节加固介绍了APi单纯Post用对称加密(Base64 为列子)加密方式,这种加密方式还是存在一定的风险,加密效率虽高,但易破解,本节将介绍怎么用非对称加密 来加解密okhttp的数据,本文采用RSA加密算法为栗子. 对称加密 对称加密是最传统的加密方式,比上非对称加密,缺少安全性,但是它依旧是用的比较多的加密方法. 对称加密采用单密钥加密方式,不论是加密还是解

手动缓存Retrofit+OkHttp响应体,不再局限于Get请求缓存

转载请标明出处: http://blog.csdn.net/iamzgx/article/details/51764848 概括 这篇博客是接着上一篇博客学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐,没看过的,建议看完上一篇再来看这篇.在上一篇博客中仅仅是简单的讲解了OkHttp的缓存问题,主要是通过http协议里面的control-cache控制缓存,而且是仅仅只能是Get请求才能缓存,如果Post请求OkHttp会让response返