RxJava+Retrofit+OkHttp+mvp

本文介绍如何使用Retrofit结合RxJava实现Android应用的网络请求功能,包括依赖添加、API配置、Model层数据获取及Presenter层业务逻辑处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加依赖
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);
}


创建Model
public 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);
}

创建Activity
public 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();

    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值