Retrofit加载post(上拉刷新,下拉加载)

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.github.bumptech.glide:glide:3.7.0'
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 'com.facebook.fresco:fresco:0.12.0'
compile 'com.facebook.fresco:animated-base-support:0.12.0'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'

权限

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

public class Api {
    public static  final String  BASE_URL="http://api.tianapi.com/keji/";
}

public interface ApiService {
   @POST
   Observable postpage(@Url String url, @QueryMap Map map);

}
 
  
 
  
public interface Imodel {
    void getJson(String url,String num,String page);
}

 
  
public class UserModel implements Imodel {
    List list;
    private OnFinish onFinish;

    public interface OnFinish{
        void OnFinishListener(ArrayList list);
    }
    public void setOnFinish(OnFinish finish){
        this.onFinish=finish;
    }
    @Override
    public void getJson(String url,String num,String page) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
        ApiService apiService = retrofit.create(ApiService.class);
        Map map = new HashMap<>();
        map.put("key","ac87b4a4e634eb4a4e96fd371490a667");
        map.put("num",num);
        map.put("page",page);
         Observable home = apiService.postpage(url, map);
        home.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                    }
                    @Override
                    public void onNext(HomeBean homeBean) {
                      list = homeBean.getNewslist();
                        Log.e("main",list.get(0).getTitle()+"123");
                        onFinish.OnFinishListener((ArrayList) list);
                    }
                });
    }
}

 
  
public interface Iview {
      void getData(ArrayList list);
}

public class Iparsenter implements UserModel.OnFinish {
    private final Iview iview;
    private final UserModel userModel;

    public Iparsenter(Iview iview) {
        this.iview = iview;
        this.userModel = new UserModel();
        userModel.setOnFinish(this);
    }

    public void setNews(String url,String page,String num){
        userModel.getJson(url,page,num);
    }
    @Override
    public void OnFinishListener(ArrayList list) {
         iview.getData(list);
    }
}


public class HomeAdapter extends RecyclerView.Adapter {
    Context context;
    List list;

    public HomeAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                context).inflate(R.layout.item_home, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position).getTitle());
    }



    @Override
    public int getItemCount() {

        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);

        }
    }
}


 
  
public class MainActivity extends AppCompatActivity  implements Iview{
    Iparsenter iparsenter;
    private XRecyclerView mRecyclerView;
    int page=40;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (XRecyclerView) findViewById(R.id.id_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        iparsenter = new Iparsenter(this);
        iparsenter.setNews(Api.BASE_URL,page+"","1");

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(MainActivity.this,"下拉刷新",Toast.LENGTH_SHORT).show();
                mRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                page++;
                iparsenter.setNews(Api.BASE_URL,""+page,"1");
                Toast.makeText(MainActivity.this,"上拉加载",Toast.LENGTH_SHORT).show();
              mRecyclerView.loadMoreComplete();
            }
        });
    }

    @Override
    public void getData(ArrayList list) {
        HomeAdapter adapter = new HomeAdapter(MainActivity.this,list);
        mRecyclerView.setAdapter(adapter);
    }
}

public class HomeBean {

    /**
     * code : 200
     * msg : success
     * newslist : [{"ctime":"2017-06-06 16:00","title":"模式思考:为什么星巴克成了印钞机?","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0606/2017060614515724.jpg","url":"http://www.chinaz.com/start/2017/0606/720004.shtml"},{"ctime":"2017-06-06 16:00","title":"佛学文化社群平台,\u201c般若\u201d要连接寺庙、僧人、善信","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0606/6363235766401474079822974.jpeg","url":"http://www.chinaz.com/start/2017/0606/720017.shtml"},{"ctime":"2017-06-06 16:00","title":"短视频不再是东北人的天下 \u201c川军\u201d已崛起","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0606/6363235842103727035736068.png","url":"http://www.chinaz.com/start/2017/0606/720028.shtml"},{"ctime":"2017-06-06 11:00","title":"中国文青创业者的最大金主:远离风口,专注慢公司","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0606/2017060610250221.jpg","url":"http://www.chinaz.com/start/2017/0606/719534.shtml"},{"ctime":"2017-06-06 10:00","title":"给程序员当经纪人,程序员客栈完成300万元天使轮融资","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0606/201706060900398654.jpg","url":"http://www.chinaz.com/start/2017/0606/719406.shtml"},{"ctime":"2017-06-06 10:00","title":"经历多次押宝失败的TOM网,可能要因政策原因而彻底断了挣扎","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0606/6363233672258475907689785.jpeg","url":"http://www.chinaz.com/start/2017/0606/719411.shtml"},{"ctime":"2017-06-05 17:00","title":"网易蜗牛读书:突破付费模式 敲碎纸电隔阂","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0605/201706051550205463.jpg","url":"http://www.chinaz.com/start/2017/0605/718537.shtml"},{"ctime":"2017-06-05 17:00","title":"五年之后,网易新闻为什么彻底放弃了\u201c有态度\u201d?","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0605/201706051600289006.jpg","url":"http://www.chinaz.com/start/2017/0605/718557.shtml"},{"ctime":"2017-06-05 17:00","title":"一年赚了知性女青年7000万元,它却是行业不擅长挣钱的公司","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0605/201706051619455958.jpg","url":"http://www.chinaz.com/start/2017/0605/718595.shtml"},{"ctime":"2017-06-05 15:00","title":"从兴起到洗牌仅用一年!谁让这场全民狂欢戛然而止?","description":"创业新闻","picUrl":"http://pic.chinaz.com/thumb/2017/0605/6363226806028247287344888.jpg","url":"http://www.chinaz.com/start/2017/0605/718364.shtml"}]
     */

    private int code;
    private String msg;
    private List 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 getNewslist() {
        return newslist;
    }

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

    public static class NewslistBean {
        /**
         * ctime : 2017-06-06 16:00
         * title : 模式思考:为什么星巴克成了印钞机?
         * description : 创业新闻
         * picUrl : http://pic.chinaz.com/thumb/2017/0606/2017060614515724.jpg
         * url : http://www.chinaz.com/start/2017/0606/720004.shtml
         */

        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;
        }
    }
}


你可能感兴趣的:(Retrofit加载post(上拉刷新,下拉加载))