Mvp Retrofit Okhttp Rxjava

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.MyAdapter;
import com.bwie.shexinhai.Presenter.ShowPresenter;
import com.bwie.shexinhai.Presenter.ShowPresenterImpl;
import com.bwie.shexinhai.View.ShowView;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.youth.banner.Banner;

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

public class MainActivity extends AppCompatActivity implements ShowView {
    private Banner banner;
    private RecyclerView recycleview;
    private List<Bean.DataBean.DefaultGoodsListBean>list;
    private LinearLayoutManager manager;
    private List<String> mListImage;
    private MyAdapter adapter;
    private ShowPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        recycleview = (RecyclerView) findViewById(R.id.myrecycle);
        banner= (Banner) findViewById(R.id.mybanner);

        manager = new LinearLayoutManager(this);
        recycleview.setLayoutManager(manager);
        //p关联v
        presenter = new ShowPresenterImpl(this);
        //p关联m 做网络请求
        presenter.relevance();

        //加载图片的方法
        banner.setImageLoader(new GlideImageLoader());
        mListImage= new ArrayList<>();
        //向图片集合中添加网网址

        mListImage.add("https://image.yunifang.com/yunifang/images/goods/2076/goods_img/170930212555714134276722977.jpg");
        mListImage.add("https://image.yunifang.com/yunifang/images/goods/temp/171011192212214258195254179.jpg");
        mListImage.add("https://image.yunifang.com/yunifang/images/goods/temp/17101119283583452898243380.jpg");
        mListImage.add("https://image.yunifang.com/yunifang/images/goods/2091/goods_img/170905172822119555059792173.jpg");

        //设置Banner图片集合
        banner.setImages(mListImage);
        //切换的时间
        banner.setDelayTime(2000);
        //启动banner
        banner.start();

                 }
    @Override
    public void showData(Bean news) {
        list =  news.getData().getDefaultGoodsList();
        adapter = new MyAdapter(MainActivity.this,list);
        recycleview.setAdapter(adapter);
    }
}

 

OnFinishListener

public interface OnFinishListener {
    void success(Bean bean);
}

 

Banner


package com.bwie.shexinhai;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        //使用glide加载图片
        Glide.with(context).load(path).into(imageView);
    }
}

 

View




public interface ShowView {
    void showData(Bean news);
}

 

RetrofitUtils 


package com.bwie.shexinhai.utils;

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 RetrofitUtils {
    private static RetrofitUtils retrofitUtils;
    private RetrofitUtils(){

    }
    public static RetrofitUtils getInstance(){
        if(retrofitUtils==null){
            synchronized (RetrofitUtils.class){
                if(retrofitUtils==null){
                    retrofitUtils=new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }

    private static Retrofit retrofit;
    public static synchronized Retrofit getRetrofit(String url){
        HttpLoggingInterceptor httpLoggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("xxx",message);
            }
        });
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient=new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .connectTimeout(500, TimeUnit.SECONDS)
                .readTimeout(500,TimeUnit.SECONDS)
                .retryOnConnectionFailure(false)
                .build();
        if(retrofit==null){
            retrofit=new Retrofit.Builder().baseUrl(url)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
    public <T>T getApiService(String url,Class<T> cl){
        Retrofit retrofit = getRetrofit(url);//得到retrofit

        return retrofit.create(cl);//返回的就是网络接口对象

    }


}

 

ShowPresenterImpl 


package com.bwie.shexinhai.Presenter;

import com.bwie.shexinhai.Bean;
import com.bwie.shexinhai.Model.ShowModel;
import com.bwie.shexinhai.Model.ShowModelImpl;
import com.bwie.shexinhai.OnFinishListener;
import com.bwie.shexinhai.View.ShowView;

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public class ShowPresenterImpl implements ShowPresenter,OnFinishListener {
    ShowView showView;
    private final ShowModel showModel;
    //初始化
    public ShowPresenterImpl(ShowView showView){
        this.showView  = showView;
        //多态
        showModel = new ShowModelImpl();


    }

    @Override
    public void relevance() {
        //p跟m关联
        showModel.getData(this);
    }

    @Override
    public void success(Bean news) {
        showView.showData(news);
    }
}

 

ShowPresenter 


package com.bwie.shexinhai.Presenter;

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public interface ShowPresenter {
    void relevance();
}

 

Model


package com.bwie.shexinhai.Model;


import com.bwie.shexinhai.ApiCls.Api;
import com.bwie.shexinhai.ApiCls.ApiService;
import com.bwie.shexinhai.Bean;
import com.bwie.shexinhai.OnFinishListener;
import com.bwie.shexinhai.utils.RetrofitUtils;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;



/**
 * Created by 佘鑫海 on 2017/12/4.
 */
public class ShowModelImpl implements ShowModel {
    @Override
    public void getData(final OnFinishListener listener) {
        ApiService apiService = RetrofitUtils.getInstance().getApiService(Api.api_data, ApiService.class);
        Observable<Bean> observable = apiService.getData();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Bean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                    @Override
                    public void onNext(Bean bean) {
                        listener.success(bean);
                    }
                });
    }
    }

 

Model


package com.bwie.shexinhai.Model;

import com.bwie.shexinhai.OnFinishListener;

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public interface ShowModel {
    void getData(OnFinishListener listener);
}

 

ApiService 


package com.bwie.shexinhai.ApiCls;


import com.bwie.shexinhai.Bean;

import retrofit2.http.GET;
import rx.Observable;

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public interface ApiService {
    @GET("umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage")
    Observable<Bean> getData();
}

 

Api


package com.bwie.shexinhai.ApiCls;

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public class Api {
    public  static  final String api_data ="http://result.eolinker.com/";
}

 

Adapter


package com.bwie.shexinhai.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwie.shexinhai.Bean;
import com.bwie.shexinhai.R;
import com.facebook.drawee.view.SimpleDraweeView;

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

/**
 * Created by 佘鑫海 on 2017/12/4.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private Context context;
    private List<Bean.DataBean.DefaultGoodsListBean> list;

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.activity_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getGoods_img()).into(holder.imgLogo);
        holder.txtName.setText(list.get(position).getGoods_name());
        holder.txtName2.setText(list.get(position).getGoods_name());
        holder.imgLogo2.setImageURI(list.get(position).getGoods_img());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public void setData(Bean bean){

        if(this.list == null){
            this.list = new ArrayList<>();
        }
        this.list.addAll(bean.getData().getDefaultGoodsList());
        notifyDataSetChanged();

    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private SimpleDraweeView imgLogo;
        private TextView txtName;
        private SimpleDraweeView imgLogo2;
        private TextView txtName2;

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

            imgLogo =  itemView.findViewById(R.id.myimage1);
            imgLogo2 =  itemView.findViewById(R.id.myimage2);
            txtName =  itemView.findViewById(R.id.mytext2);
            txtName2 =  itemView.findViewById(R.id.mytext);
        }
    }
}

 

  compile ‘com.android.support:appcompat-v7:26.+‘
    compile ‘com.youth.banner:banner:1.4.9‘
    compile ‘com.github.bumptech.glide:glide:4.0.0‘
    compile ‘com.android.support:recyclerview-v7:21.0.+‘
    compile ‘com.facebook.fresco:fresco:0.14.1‘
    compile ‘com.squareup.retrofit2:retrofit:2.3.0‘
    compile ‘com.squareup.retrofit2:adapter-rxjava:2.0.1‘
    compile ‘io.reactivex:rxandroid:1.1.0‘
    compile ‘com.squareup.okhttp3:okhttp:3.0.0‘
    compile ‘com.squareup.okio:okio:1.5.0‘
    compile ‘com.squareup.okhttp3:logging-interceptor:3.4.1‘
    compile ‘com.squareup.retrofit2:converter-gson:2.3.0‘
    compile ‘com.android.support.constraint:constraint-layout:1.0.2‘
    testCompile ‘junit:junit:4.12‘

 

以上是关于Mvp Retrofit Okhttp Rxjava的主要内容,如果未能解决你的问题,请参考以下文章

[Android] 转-RxJava+MVP+Retrofit+Dagger2+Okhttp大杂烩

Mvp+Rxjava+Retrofit2.0+Okhttp完善版

MVP+Retrofit+RxJava探索之路

Retrofit源码分析以及MVP框架封装使用

一款最流行的MVPArms MVP快速集成框架Retoift,Okhttp,RxCache,Gson,RxLifeCycle, Dagger2,Rxjava,ImageLoader

Glide工具类/OkGo网络请求/MVP请求数据/Retrofit+RxJava/ARouter/补间动画/轮播图等等等等等 记录一下~~