1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > android+MVP+Retrofit+RxJava做网络请求

android+MVP+Retrofit+RxJava做网络请求

时间:2021-09-04 06:44:31

相关推荐

android+MVP+Retrofit+RxJava做网络请求

1.MVP+Retrofit+RxJava做网络请求2.GreenDao把第一页的数据缓存到数据库,列表实现上拉加载更多,下拉刷新功能

3.列表中的图片(img字段)使用Fresco进行加载

4.RecyclerView列表适配器做优化

上面是需求,下面奉献上代码

1.依赖:

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.jcodecraeer:xrecyclerview:1.3.2'// Fresco所需依赖compile 'com.facebook.fresco:fresco:0.12.0'

2.创建MVP包

model包下创建

ApiService接口

package .app1.model;import java.util.List;import .app1.bean.Bean;import retrofit2.http.GET;import retrofit2.http.Url;import rx.Observable;/** * Created by Administrator on /11/12. */public interface ApiService {@GET Observable<List<Bean>> getpage(@Url String url);}

Imodel接口

package .app1.model;/** * Created by Administrator on /11/12. */public interface Imodel {void getpage(String url);}

PageModel类

package .app1.model;import java.util.List;import .app1.bean.Bean;import .app1.presenter.Api;import retrofit2.Retrofit;import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;import retrofit2.converter.gson.GsonConverterFactory;import rx.Observable;import rx.Observer;import rx.android.schedulers.AndroidSchedulers;import rx.schedulers.Schedulers;/** * Created by TA on /11/11. */public class PageModel implements Imodel {private OnFinish onFinish ;public interface OnFinish{void OnFinishListener(List<Bean.DataBean> list);}public void setOnFinish(OnFinish onFinish ){this.onFinish = onFinish;}@Override public void getpage(String url) {Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();ApiService apiService = retrofit.create(ApiService.class);Observable<List<Bean>> page = apiService.getpage(url);page.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<List<Bean>>() {@Overridepublic void onCompleted() {}@Overridepublic void onError(Throwable e) {}@Overridepublic void onNext(List<Bean> bean) {List<Bean.DataBean> data = bean.get(0).getData();onFinish.OnFinishListener(data);}});}}

view包下

Iview接口

package .app1.view;import java.util.List;import .app1.bean.Bean;/** * Created by Administrator on /11/12. */public interface Iview {void getpage(List<Bean.DataBean> list);}

XrAdapter类

package .app1.view;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import com.facebook.drawee.view.SimpleDraweeView;import com.jcodecraeer.xrecyclerview.XRecyclerView;import java.util.List;import .app1.R;import .app1.bean.Bean;/** * Created by Administrator on /11/12. */public class XrAdapter extends XRecyclerView.Adapter<XrAdapter.ViewHolder> {Context context;List<Bean.DataBean> list;public XrAdapter(Context context, List<Bean.DataBean> list) {this.context = context;this.list = list;}@Override public XrAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//给Adapter添加布局,bq把这个view传递给HoldView,让HoldView找到空间 View view= LayoutInflater.from(context).inflate(R.layout.xr_item, parent,false);ViewHolder holdView=new ViewHolder(view);return holdView;}@Override public void onBindViewHolder(XrAdapter.ViewHolder holder, int position) {holder.iv.setImageURI(list.get(position).getImg());holder.tv.setText(list.get(position).getTitle());}@Override public int getItemCount() {return list.size();}public class ViewHolder extends RecyclerView.ViewHolder {private final TextView tv;private final SimpleDraweeView iv;public ViewHolder(View itemView) {super(itemView);tv = itemView.findViewById(R.id.tv);iv = itemView.findViewById(R.id.iv);}}}

presnter包下

Api类

package .app1.presenter;/** * Created by Administrator on /11/12. */public class Api {public static final String url = "/";}

PagePresenter类

package .app1.presenter;import java.util.List;import .app1.bean.Bean;import .app1.model.PageModel;import .app1.view.Iview;/** * Created by Administrator on /11/12. */public class PagePresenter implements PageModel.OnFinish{private final Iview iview;private final PageModel pageModel ;public PagePresenter(Iview iview) {this.iview = iview;this.pageModel = new PageModel() ;}public void getpage(String url){pageModel.setOnFinish(this);pageModel.getpage(url);}@Override public void OnFinishListener(List<Bean.DataBean> list) {iview.getpage(list);}}

MyApp类

package .app1;import android.app.Application;import com.facebook.drawee.backends.pipeline.Fresco;public class MyApp extends Application {@Override public void onCreate() {super.onCreate();Fresco.initialize(this);}}

MainActivity类

package .app1;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.LinearLayoutManager;import android.widget.Toast;import com.jcodecraeer.xrecyclerview.XRecyclerView;import java.util.List;import .app1.bean.Bean;import .app1.presenter.PagePresenter;import .app1.view.Iview;import .app1.view.XrAdapter;public class MainActivity extends AppCompatActivity implements Iview {private XRecyclerView xr;private XrAdapter xa;int page = 1;String url = "wap/data/news/txs/page_1.json";@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {final PagePresenter pagePresenter = new PagePresenter(this) ;pagePresenter.getpage(url);xr = (XRecyclerView) findViewById(R.id.xr);LinearLayoutManager layoutManager=new LinearLayoutManager(this);layoutManager.setOrientation(LinearLayoutManager.VERTICAL);xr.setLayoutManager(layoutManager);xr.setLoadingListener(new XRecyclerView.LoadingListener() {@Override public void onRefresh() {page=1;url = "wap/data/newss/page_" + page + ".json";pagePresenter.getpage(url);xa.notifyDataSetChanged();Toast.makeText(MainActivity.this , "刷新成功!" , Toast.LENGTH_SHORT).show();xr.refreshComplete();}@Override public void onLoadMore() {page++;url = "wap/data/news/txs/page_"+page+".json";pagePresenter.getpage(url);// xa.notifyDataSetChanged();Toast.makeText(MainActivity.this , "加载成功!" , Toast.LENGTH_SHORT).show();xr.loadMoreComplete();}});}@Override public void getpage(List<Bean.DataBean> list) {xa = new XrAdapter(this , list);xr.setAdapter(xa);}}Bean类

package .app1.bean;import java.util.List;/** * Created by TA on /11/11. */public class Bean {/*** data : [{"summary":"","img":"/wap/style/data/news/txs/images/1510358537754.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239715.json","countid":20009,"id":"239715","title":"【环球】道指终结八周连涨走势 迅雷收涨近40%","otime":"-11-11 08:02:40","source":"","views":"1085","resType":"置顶"},{"summary":"","img":"/wap/style/data/news/txs/images/1510360081605.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239719.json","countid":20009,"id":"239719","title":"互金协会:从事外汇、贵金属等杠杆交易的网络平台属非法设立","otime":"-11-11 08:29:40","source":"","views":"14","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510359764521.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239718.json","countid":20009,"id":"239718","title":"国土部整顿70城囤地行为:严打坐地生财 苏州被点名","otime":"-11-11 08:23:40","source":"","views":"11","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510359068238.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239717.json","countid":20009,"id":"239717","title":"第六轮脱欧谈判无果而终 欧盟要求英国两周破局","otime":"-11-11 08:16:55","source":"","views":"17","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510358907282.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239716.json","countid":20009,"id":"239716","title":"黄金再现神秘天量交易 10分钟内400万盎司砸盘","otime":"-11-11 08:16:25","source":"","views":"127","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510321834470.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239713.json","countid":20009,"id":"239713","title":"11月10日晚间财经要闻&公告精选汇总","otime":"-11-10 21:56:03","source":"","views":"67611","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510318706602.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239712.json","countid":20009,"id":"239712","title":"5G概念股牛气冲天,但这几只股更牛,你可能已经想到了!","otime":"-11-10 20:58:50","source":"","views":"81532","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510317235707.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239711.json","countid":20009,"id":"239711","title":"证监会核发7家企业IPO批文,筹资总金额不超过32亿元","otime":"-11-10 20:34:10","source":"","views":"59553","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510317079914.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239710.json","countid":20009,"id":"239710","title":"证监会:今日5家公司首发上会3家过会","otime":"-11-10 20:32:20","source":"","views":"22955","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510313338777.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239708.json","countid":20009,"id":"239708","title":"复星:郭广昌辞任复星高科董事长,但仍是复星集团董事长","otime":"-11-10 19:30:17","source":"","views":"6","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510312053854.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239705.json","countid":20009,"id":"239705","title":"乐视网:贾跃亭称已将减持所得资金全部用于非上市体系以及本人所涉及的债务偿付等","otime":"-11-10 19:08:00","source":"","views":"53189","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510311245055.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239704.json","countid":20009,"id":"239704","title":"上交所发布《关于做好落实减持新规相关信息填报工作的通知》","otime":"-11-10 18:54:40","source":"","views":"16161","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510309722002.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239700.json","countid":20009,"id":"239700","title":"【龙虎榜】4家机构合计6.62亿抱团出逃雅克科技","otime":"-11-10 18:46:09","source":"","views":"40048","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510309648056.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239699.json","countid":20009,"id":"239699","title":"券商强力推荐!下周热点板块与个股看这里!","otime":"-11-10 18:31:13","source":"","views":"96094","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510309427986.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239698.json","countid":20009,"id":"239698","title":"11月10日晚间机构研报精选:10股值得关注","otime":"-11-10 18:29:09","source":"","views":"86312","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510309350965.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239697.json","countid":20009,"id":"239697","title":"投保基金:10月证券市场投资者信心指数53.8,环比下降7.4%","otime":"-11-10 18:28:36","source":"","views":"17122","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510306033350.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239691.json","countid":20009,"id":"239691","title":"媒体:证监会三大机制严管IPO审核,交易所将介入现场检查","otime":"-11-10 17:27:46","source":"","views":"33174","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510304856330.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239688.json","countid":20009,"id":"239688","title":"十大博客看后市:A股赚大钱的机会来了!","otime":"-11-10 18:30:40","source":"","views":"160270","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510304786745.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239687.json","countid":20009,"id":"239687","title":"昨晚说要增持,今早就涨停,增持计划那么神?这些行为你要小心!","otime":"-11-10 19:30:57","source":"","views":"150834","resType":""},{"summary":"","img":"/wap/style/data/news/txs/images/1510304675355.jpg","advTypeShare":"","url":"/wap/data/news/txs//11/239686.json","countid":20009,"id":"239686","title":"双11物流大战提前开打,各大电商都有哪些杀手锏?","otime":"-11-10 17:23:54","source":"","views":"17369","resType":""}]* header : {"pagesize":20,"last":"page_10.json","pre":"page_1.json","next":"page_2.json","totalsize":186,"first":"page_1.json","totalpage":10}*/ private HeaderBean header;private List<DataBean> data;public HeaderBean getHeader() {return header;}public void setHeader(HeaderBean header) {this.header = header;}public List<DataBean> getData() {return data;}public void setData(List<DataBean> data) {this.data = data;}public static class HeaderBean {/** * pagesize : 20 * last : page_10.json * pre : page_1.json * next : page_2.json * totalsize : 186 * first : page_1.json * totalpage : 10 */ private int pagesize;private String last;private String pre;private String next;private int totalsize;private String first;private int totalpage;public int getPagesize() {return pagesize;}public void setPagesize(int pagesize) {this.pagesize = pagesize;}public String getLast() {return last;}public void setLast(String last) {this.last = last;}public String getPre() {return pre;}public void setPre(String pre) {this.pre = pre;}public String getNext() {return next;}public void setNext(String next) {this.next = next;}public int getTotalsize() {return totalsize;}public void setTotalsize(int totalsize) {this.totalsize = totalsize;}public String getFirst() {return first;}public void setFirst(String first) {this.first = first;}public int getTotalpage() {return totalpage;}public void setTotalpage(int totalpage) {this.totalpage = totalpage;}}public static class DataBean {/** * summary : * img : /wap/style/data/news/txs/images/1510358537754.jpg * advTypeShare : * url : /wap/data/news/txs//11/239715.json * countid : 20009 * id : 239715 * title : 【环球】道指终结八周连涨走势 迅雷收涨近40% * otime : -11-11 08:02:40 * source : * views : 1085 * resType : 置顶 */ private String summary;private String img;private String advTypeShare;private String url;private int countid;private String id;private String title;private String otime;private String source;private String views;private String resType;public String getSummary() {return summary;}public void setSummary(String summary) {this.summary = summary;}public String getImg() {return img;}public void setImg(String img) {this.img = img;}public String getAdvTypeShare() {return advTypeShare;}public void setAdvTypeShare(String advTypeShare) {this.advTypeShare = advTypeShare;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public int getCountid() {return countid;}public void setCountid(int countid) {this.countid = countid;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getOtime() {return otime;}public void setOtime(String otime) {this.otime = otime;}public String getSource() {return source;}public void setSource(String source) {this.source = source;}public String getViews() {return views;}public void setViews(String views) {this.views = views;}public String getResType() {return resType;}public void setResType(String resType) {this.resType = resType;}}}布局:activity_main

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android" xmlns:tools="/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".app1.MainActivity"><com.jcodecraeer.xrecyclerview.XRecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/xr" ></com.jcodecraeer.xrecyclerview.XRecyclerView></RelativeLayout>

xr_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" xmlns:fresco="/apk/res-auto"><com.facebook.drawee.view.SimpleDraweeView android:id="@+id/iv" android:layout_width="100dp" android:layout_height="100dp" fresco:actualImageScaleType="focusCrop" fresco:failureImage="@mipmap/ic_launcher" fresco:failureImageScaleType="centerInside" fresco:placeholderImage="@mipmap/ic_launcher" fresco:placeholderImageScaleType="fitCenter" fresco:progressBarAutoRotateInterval="1000" fresco:progressBarImage="@mipmap/ic_launcher" fresco:progressBarImageScaleType="centerInside" fresco:retryImage="@mipmap/ic_launcher" fresco:retryImageScaleType="centerCrop" fresco:roundAsCircle="false" fresco:viewAspectRatio="1.6" /><TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/iv" android:layout_toEndOf="@+id/iv" android:layout_marginLeft="18dp" android:layout_marginStart="18dp" /></LinearLayout>

最后清单文件加权限

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

android:name=".MyApp"这句话必须加否则会没有数据的

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。