MVP +Retrofit +Rxjava 请求

今天我们一起来搭建  mvp + retrofit + rxjava 框架

先导入依赖

    compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.facebook.fresco:fresco:1.5.0'
    compile 'com.youth.banner:banner:1.4.9'
    compile 'com.github.bumptech.glide:glide:4.0.0'
    compile 'com.github.leifzhang:IjkLib:0.4.3'
    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'
    compile 'com.facebook.fresco:fresco:1.5.0'
    compile 'org.greenrobot:greendao:3.2.0'
    compile 'org.greenrobot:greendao-generator:3.2.0'
    compile 'com.getbase:floatingactionbutton:1.10.1'
    compile 'fm.jiecao:jiecaovideoplayer:5.5'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

   (在使用 jiecaovideoplayer:5.5'饺子视频播放的时候要把minSdkVersion 改成16)

 //清单文件
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

PS: 还有name="" 哦!

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.youth.banner.Banner
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/ban"/>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/rvnn"/>

</LinearLayout>


//item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal"
    >
    <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:id="@+id/video_view" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/con"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/Tiel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

//bean包下的
public class InfoBean {


    private int code;
    private String msg;
    private List<DataBean> data;

    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<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {


        private String content;
        private int id;
        private String image_url;
        private String title;
        private int type;
        private String vedio_url;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getImage_url() {
            return image_url;
        }

        public void setImage_url(String image_url) {
            this.image_url = image_url;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getVedio_url() {
            return vedio_url;
        }

        public void setVedio_url(String vedio_url) {
            this.vedio_url = vedio_url;
        }
    }
}

//适配器

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.bwei.Bean.InfoBean;
import com.bwei.month3.R;

import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<InfoBean.DataBean> list;


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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);

        return new Viewitem(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        Viewitem viewitem= (Viewitem) holder;

        viewitem.con.setText(list.get(position).getContent());
        viewitem.tie.setText(list.get(position).getTitle());

        String vedio_url=list.get(position).getVedio_url();
        viewitem.view.setUp(vedio_url+"",viewitem.view.SCREEN_LAYOUT_NORMAL,list.get(position).getTitle());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class Viewitem extends RecyclerView.ViewHolder{

        private final JCVideoPlayerStandard view;
        private final TextView con;
        private final TextView tie;


        public Viewitem(View itemView) {
            super(itemView);

            view=itemView.findViewById(R.id.video_view);
            con=itemView.findViewById(R.id.con);
            tie=itemView.findViewById(R.id.Tiel);
        }
    }
}

//myapp下的App
public class App extends Application{

    @Override
    public void onCreate() {
        super.onCreate();

        Fresco.initialize(this);
    }
}

//net包下的API

public class Api {

    public static final boolean isOnline = false;
    public static final String devIP = "http://result.eolinker.com";
    public static final String workIP = "";
    public static final String HOST = isOnline ? workIP  : devIP;
    public static final String LOG=HOST+"/";
}

//ApiService

public interface ApiService {


    @GET("iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9")
    Observable<InfoBean> getNoParams(@Query("uri")String uri);
}

//RetrofitUtil


import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtil {
    private static RetrofitUtil retrofitUtil;
    private RetrofitUtil(){

    }
    public static RetrofitUtil getInstance(){
        if(retrofitUtil==null){
            synchronized (RetrofitUtil.class){
                if(retrofitUtil==null){
                    retrofitUtil=new RetrofitUtil();
                }
            }
        }
        return  retrofitUtil;
    }
    private static Retrofit retrofit;
    public static  synchronized  Retrofit getRetrofit(String url){
        HttpLoggingInterceptor tag = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("TAG", message);
            }
        });
        tag.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(tag).connectTimeout(5000, TimeUnit.SECONDS).build();
        if(retrofit==null){
            retrofit= new Retrofit.Builder().client(okHttpClient).baseUrl(url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
        }
        return retrofit;
    }
    public <T> T getApiService(String url,Class<T> clazz){
        Retrofit retrofit = getRetrofit(url);
        return  retrofit.create(clazz);
    }
}


//MOdel层的IShowModel
public interface IShowModel {

    void onSuexxc(InfoBean infoBean);
}

//ShowModel

public class ShowModel {

    private Context context;

    public ShowModel(Context context) {
        this.context = context;
    }

    public void zhu(final IShowModel iShowModel){
        Retrofit retrofit = RetrofitUtil.getRetrofit(Api.LOG);
        ApiService apiService = retrofit.create(ApiService.class);
        Observable<InfoBean> observable = apiService.getNoParams("vedio");
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<InfoBean, InfoBean>() {
                    @Override
                    public InfoBean call(InfoBean disanBean) {
                        return disanBean;
                    }
                }).subscribe(new Subscriber<InfoBean>() {


            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(InfoBean infoBean) {

                iShowModel.onSuexxc(infoBean);

            }
        });
    }
}

//presenter包下的ShowPersenter

import android.content.Context;

import com.bwei.Bean.InfoBean;
import com.bwei.Model.IShowModel;
import com.bwei.Model.ShowModel;
import com.bwei.View.ShowView;

public class ShowPersenter {

    private Context context;
    private ShowView showView;
    private final ShowModel showModel;


    public ShowPersenter(Context context, ShowView showView) {
        this.context = context;
        this.showView = showView;
        showModel=new ShowModel(context);
    }

    public void getWin(){

        showModel.zhu(new IShowModel() {
            @Override
            public void onSuexxc(InfoBean infoBean) {
                showView.getBEan(infoBean);
            }
        });
    }

}

//View层的ShowView

public interface ShowView {

    public void getBEan(InfoBean infoBean);
}

//MainActivity


import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bwei.Adapter.MyAdapter;
import com.bwei.Bean.InfoBean;
import com.bwei.Presenter.ShowPersenter;
import com.bwei.View.ShowView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class MainActivity extends AppCompatActivity implements ShowView {

    private Banner mBan;
    private List<String> list = new ArrayList<String>();
    private ShowPersenter showPersenter;
    private MyAdapter adapter;
    private RecyclerView mRvnn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        showPersenter.getWin();
    }

    private void initView() {
        mBan = (Banner) findViewById(R.id.ban);
        showPersenter = new ShowPersenter(this, this);
        mRvnn = (RecyclerView) findViewById(R.id.rvnn);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        mRvnn.setLayoutManager(linearLayoutManager);
    }


    public class Glider extends ImageLoader{

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into((ImageView) imageView);
        }
    }

    @Override
    public void onBackPressed() {

        if (JCVideoPlayer.backPress()){

            return;
        }

        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();

        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void getBEan(InfoBean infoBean) {

        List<InfoBean.DataBean> data=infoBean.getData();
        adapter = new MyAdapter(MainActivity.this, data);
        mRvnn.setAdapter(adapter);

        for(int i=0;i<data.size();i++){

            list.add(data.get(i).getImage_url()+"");
        }

        mBan.setImageLoader(new Glider());
        mBan.setDelayTime(2000);
        mBan.setImages(list);
        mBan.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        mBan.start();

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值