效果图我就不展示了,主要就是把Retrofit+Rxjava+OkHttp封装在了一起,废话不多说,甩代码
请求接口,我使用的是GET
public interface Inters {
/**
* http://api.tianapi.com/nba/?key=71e58b5b2f930eaf1f937407acde08fe&num=10
*/
@GET("/nba")
Observable<Bean> getBean(@Query("key") String key,@Query("num") int num);
}
Retrofit+Rxjava+OkHttp封装
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitUnitl {
private Retrofit mRetrofit;
private String baseUrl;
OkHttpClient client;
private static RetrofitUnitl mRetrofitManager;
private RetrofitUnitl(String baseUrl,OkHttpClient client){
this.baseUrl=baseUrl;
this.client=client;
initRetrofit();
}
public static synchronized RetrofitUnitl getInstance(String baseUrl,OkHttpClient client){
if (mRetrofitManager == null){
mRetrofitManager = new RetrofitUnitl(baseUrl,client);
}
return mRetrofitManager;
}
private void initRetrofit() {
mRetrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
}
public <T> T setCreate(Class<T> reqServer) {
return mRetrofit.create(reqServer);
}
}
下面开始使用MVP请求数据
V层
public interface ILogView {
//请求成功
void onSucc(Bean bean);
}
M层
import okhttp3.OkHttpClient;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class LogModel {
ScuMod scuMod;
public void setScuMod(ScuMod scuMod) {
this.scuMod = scuMod;
}
public void Succes(){
//OkHttp里面可以添加拦截器
OkHttpClient ok = new OkHttpClient.Builder()
.build();
//请求数据
RetrofitUnitl.getInstance("http://api.tianapi.com",ok)
.setCreate(Inters.class)
.getBean("71e58b5b2f930eaf1f937407acde08fe",10)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<Bean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Bean bean) {
scuMod.MScus(bean);
}
});
}
//定义一个接口
public interface ScuMod{
void MScus(Bean bean);
}
}
P层
public class LogPresenter implements LogModel.ScuMod {
//弱引用解绑
WeakReference<ILogView> view;
LogModel model;
public LogPresenter( ILogView view) {
attach(view);
model = new LogModel();
model.setScuMod(this);
}
@Override
public void MScus(Bean bean) {
view.get().onSucc(bean);
}
//我们在写一个方法拿到我们的请求
public void ShowPer(){
model.Succes();
}
//绑定
public void attach(ILogView views){
view = new WeakReference(views);
}
//解绑方法
public void detach(){
view.clear();
}
}
Bean包和适配器我就省略了,直接写主布局
public class MainActivity extends AppCompatActivity implements ILogView {
private LogPresenter presenter;
public RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
//调用P层
presenter = new LogPresenter(this);
presenter.ShowPer();
}
@Override
public void onSucc(Bean bean) {
//线性布局
LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
rv.setLayoutManager(manager);
//拿到适配器
MyAdapter adapter = new MyAdapter(MainActivity.this,bean);
rv.setAdapter(adapter);
}
//调用P层的解绑
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detach();
}
}
最后是依赖,不要导错依赖
compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.android.support:recyclerview-v7:26.1.0' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.1.7' compile 'io.reactivex:rxjava:1.1.1' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'