RxJava+Retrofit+OkHttp+mvp

来源:互联网 发布:网上教育软件 编辑:程序博客网 时间:2024/05/29 19:25
添加依赖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.squareup.okhttp3:okhttp:3.2.0'    compile 'com.squareup.okio:okio:1.7.0'
写个Api类public class Api {        public static final String SHOW = "http://www.meirixue.com";}

写个接口public interface ApiService {    /**     * http://www.meirixue.com/api.php?c=index&a=index     */    @GET("/api.php")    Observable<ShowBean> getShow(@Query("c") String index,@Query("a") String index2);}

创建Modelpublic class ShowModel {    public void getNet(final ShowListenerSuccess showListenerSuccess, String indx, String index2){        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);        OkHttpClient okHttpClient = new OkHttpClient.Builder()                .addInterceptor(loggingInterceptor)                .build();        Retrofit build = new Retrofit.Builder().baseUrl(Api.SHOW)                .client(okHttpClient)                .addConverterFactory(GsonConverterFactory.create())                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                .build();        ApiService apiService = build.create(ApiService.class);                Observable<ShowBean> observable = apiService.getShow(indx, index2);        observable.subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Subscriber<ShowBean>() {                    @Override                    public void onCompleted() {                    }                    @Override                    public void onError(Throwable e) {                    }                    @Override                    public void onNext(ShowBean showBean) {                        showListenerSuccess.showSucdess(showBean);                    }                });    }}

创建Presenter类public class ShowPresenter implements ShowListenerSuccess{    private ShowListener showListener;    private final ShowModel model;    public ShowPresenter(ShowListener showListener){        this.showListener = showListener;        model = new ShowModel();    }    public void relevance(String index,String index2){        model.getNet(this,index,index2);    }    @Override    public void showSucdess(ShowBean showBean) {        showListener.showView(showBean);    }}

创建View接口public interface ShowListener {    public void showView(ShowBean showBean);}

成功回调接口public interface ShowListenerSuccess {    public void showSucdess(ShowBean showBean);}

创建Activitypublic class MainActivity extends AppCompatActivity implements ShowListener{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ShowPresenter presenter = new ShowPresenter(this);        presenter.relevance("index","index");    }    @Override    public void showView(ShowBean showBean) {        String id = showBean.getData().getAdlist().get(0).getId();        Toast.makeText(this, ""+id, Toast.LENGTH_SHORT).show();    }}