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);
}