Retrofit+RXJava封装

MainActivity

package wanghuiqi.bawie.com.retrofitutils;

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

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import wanghuiqi.bawie.com.retrofitutils.presenter.DataPresenter;
import wanghuiqi.bawie.com.retrofitutils.view.DataView;

public class MainActivity extends AppCompatActivity implements DataView {

    private RecyclerView recy;
    private MyAdapter myAdapter;

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

        myAdapter = new MyAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recy.setLayoutManager(linearLayoutManager);
        recy.setAdapter(myAdapter);

        HttpUtilsManager.getInstance().init();
        Observable<News> newsInfo = HttpUtilsManager.getService().getNewsInfo(39, 1);

        newsInfo.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<News>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(News news) {
                        List<News.DataBean> data = news.getData();
                        myAdapter.setData(data);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initView() {
        recy = (RecyclerView) findViewById(R.id.recy);
    }

    @Override
    public void OnDataView(List<News.DataBean> data) {
        myAdapter.setData(data);
    }
}

HttpUtilsManager

package wanghuiqi.bawie.com.retrofitutils;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpUtilsManager {
    private static HttpUtilsManager mHttpUtilsManager;
    private static Retrofit retrofit;
    private static volatile HttpService mHttpService=null;

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

    public Observable<News> init() {
        OkHttpClient httpClient = new OkHttpClient.Builder().build();
        retrofit = new Retrofit.Builder()
                .client(httpClient)
                .baseUrl(NetUrl.url)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return null;

    }

    public static HttpService getService(){
        if (mHttpService==null){
            synchronized (HttpService.class){
                mHttpService=retrofit.create(HttpService.class);
            }
        }
        return mHttpService;
    }
}

HttpService

public interface HttpService {

    @GET("getProducts")
    Observable<News> getNewsInfo(@Query("pscid") int pscid, @Query("page") int page);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值