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‘
时间: 2024-10-17 08:45:08

Mvp Retrofit Okhttp Rxjava的相关文章

RecyclerView+Retrofit+OkHttp+RxJava

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

Rxjava+ReTrofit+okHttp深入浅出-终极封装

Rxjava+ReTrofit+okHttp深入浅出-终极封装 背景: 学习Rxjava和retrofit已经很长时间了,功能确实很强大,但是使用起来还是有点复杂,代码的重复性太高,所以决定把基于retrofit和rxjava的处理统一封装起来,实现的功能: 1.Retrofit+Rxjava+okhttp基本使用方法 2.统一处理请求数据格式 3.统一的ProgressDialog和回调Subscriber处理 4.取消http请求 5.预处理http请求 5.返回数据的统一判断 效果: 封装

设计模式笔记之四:MVP+Retrofit+RxJava组合使用

本博客转自郭霖公众号:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650236866&idx=1&sn=da666831f67303eeb7a57c1591204b43&scene=24&srcid=0910wJAKSLdsEFTBKwKfTNor#wechat_redirect http://blog.csdn.net/qq_27778869/article/details/52121208 MVP

Rxjava+ReTrofit+okHttp深入浅出-终极封装四(多文件下载之断点续传)

Rxjava+ReTrofit+okHttp深入浅出-终极封装四(多文件下载之断点续传) 背景 断点续传下载一直是移动开发中必不可少的一项重要的技术,同样的Rxjava和Retrofit的结合让这个技术解决起来更加的灵活,我们完全可以封装一个适合自的下载框架,简单而且安全! 效果 实现 下载和之前的http请求可以相互独立,所以我们单独给download建立一个工程moudel处理 1.创建service接口 和以前一样,先写接口 注意:Streaming是判断是否写入内存的标示,如果小文件可以

手把手带你走进MVP +Dagger2 + DataBinding+ Rxjava+Retrofit 的世界

0.0 Android开发现在的变化用一个词来形容就是 :翻天覆地 越来越多的项目使用了MVP + Rxjava+Retrofit +Dagger2 + DataBinding等等东西.. 但是这些东西对于木有用过的同学们开起来还是比较头疼的. 转载请标明出处:http://blog.csdn.net/wingichoy/article/details/51981756 网上有很多介绍他们的教程,但是都比较详细(我听到有童鞋问:详细还不好?? 其实他们最好的学习方式还是边敲边踩坑边学,一下介绍的

学会Retrofit+OkHttp+RxAndroid三剑客的使用,让自己紧跟Android潮流的步伐

转载请标明出处: http://blog.csdn.net/iamzgx/article/details/51607387 概括 在上一篇博客android网络框架OkHttp之get请求(源码初识) 讲解了OkHttp的简单使用和OkHttp源码的分析,主要讲解的还是理论上的知识,还是没有去实践下,那么这篇博客里面就来实践下.在上一篇博客里面说到了OkHttp类似HttpUrlConnection.按这样说的话,我们在项目中肯定还是要封装一层.如果嫌封装麻烦的话,也可以拿来主义,比如使用鸿洋大

Android探索之基于okHttp打造自己的网络请求&lt;Retrofit+Okhttp&gt;(五)

前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少收有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下Retrofit+Okhttp的搭配使用. Retrofit介绍: Retrofit和okHttp师出同门,也是Square的开源库,它是一个类型安全的网络请求库,Retrofit简化了网络请求流程,基于OkHtttp做了封装,解耦的更彻底:比方说通过注解来配置请求参数,通过工厂来生成CallAdap

Retrofit结合RxJava使用说明

PS:年后一直没有更新博客,实在是太忙了.今天周六也是终于抽出了时间,就决定写一篇Retrofit结合RxJava使用说明.刚好现在写的的项目中也有用到,趁热打铁简单说说.最近也不知道博客写什么内容,大家有什么想了解的内容,也可以评论告诉我,我尽力解答. 如果对Retrofit不了解可以看我之前的一篇博客:Retrofit 2.0使用 ,这次的内容也是在此基础上的.关于RxJava可以参考:学习RxJava(资料汇总),这里我就不多说了. 1.准备工作 我们需要添加相应的依赖: compile

Retrofit+OKHttp 教你怎么持久化管理Cookie

绪论 最近小编有点忙啊,项目比较紧,所以一直在忙活项目,继之前的自定义组件之后就没再写博客了,如果你没看到之前的自定义组件你可以看一下: Android自定义下拉刷新动画–仿百度外卖下拉刷新 Android自定义组合控件-教你如何自定义下拉刷新和左滑删除 效果还行,源码也已经传到我的Github上了. 那么今天小编来给大家分享点什么呢?对,就是它:Retrofit,话说Retrofit最近真的很火啊,Retrofit+OKHttp现在似乎已经成为了Android网络请求框架的主流框架了吧,小编之