无聊的时候自己写写,大神勿扰。
首先导入依赖
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'创建API类
例如:
public class api { public static final String HOME_URL="http://api.expoon.com/AppNews/getNewsList/"; }
然后创建api接口
public interface ApiService { @GET("type/1/p/1") Observable<Homes> getHomes(); }在Main方法里面得到 接口中的方法
public void getHomes() { Retrofit retrofit = new Retrofit.Builder().baseUrl(api.HOME_URL).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); ApiService apiserver = retrofit.create(ApiService.class); rx.Observable<Homes> homes =apiserver.getHomes(); homes.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Homes>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Homes homes) { for (int i=0;i<homes.getData().size();i++){ String name =homes.getData().get(i).getNews_title(); String imgs = homes.getData().get(i).getPic_url(); imglist.add(imgs); namelist.add(name); } Myreadapter adaptera = new Myreadapter(getActivity(),namelist,imglist); RV.setAdapter(adaptera); RV.setLayoutManager(new GridLayoutManager(getActivity(), 1)); } }); }最重要的事权限问题