码迷,mamicode.com
首页 > 编程语言 > 详细

RecyclerView+Retrofit+OkHttp+RxJava

时间:2017-12-06 10:31:35      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:schedule   edr   accept   factor   static   ram   .sh   url   builder   

*图片初始化
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;
}
}
}
 
 





RecyclerView+Retrofit+OkHttp+RxJava

标签:schedule   edr   accept   factor   static   ram   .sh   url   builder   

原文地址:http://www.cnblogs.com/ysxy/p/7991309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!