Rotrofit的简单使用

来源:互联网 发布:centos更新python 编辑:程序博客网 时间:2024/04/30 03:32

加入Rotrofit类库
compile ‘com.android.support:appcompat-v7:25.1.0’
compile ‘com.squareup.retrofit2:retrofit:2.0.1’
compile ‘com.squareup.retrofit2:converter-gson:2.2.0’//json转换器
compile ‘com.squareup.picasso:picasso:2.5.2’//加载图片
compile ‘com.squareup.retrofit2:adapter-rxjava2:2.2.0’//支持Rxjava调用
compile ‘io.reactivex.rxjava2:rxandroid:2.0.1’//Rxjava增加对android支持
compile ‘io.reactivex.rxjava2:rxjava:2.0.7’//Rxjava异步请求库

定义一个接口
public interface Isenond {

 //  @POST("")//  @FormUrlEncoded//post请求需要加上这句@Field提交注解参数//  Call<ResuteBean> getList(@Path("categroe") String categroe, @Field("id") int id,@Field("name") int name, @Field("row") int row);暂时只用了get请求 @Query传递查询参数注解@GET("/api/{categroe}/list")//get请求拼接baseurl的url {categroe}是可以动态替换的url参数Call<ResuteBean> getList(@Path("categroe") String categroe, @Query("id") int id,  @Query("name") int name, @Query("row") int row);}public class seconActivity extends AppCompatActivity {private ListView listView;private List<ResuteBean.TngouBean>list1=new ArrayList<ResuteBean.TngouBean>();private ThreeAdapter adapter;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.seconlayout);       listView= (ListView) findViewById(R.id.listview);    adapter = new ThreeAdapter(list1,this);    listView.setAdapter(adapter);    Retrofit retrofit = new Retrofit.Builder()            .baseUrl("http://www.tngou.net")            .addConverterFactory(GsonConverterFactory.create())//加入json自动转换工厂            .build();    Isenond isenond = retrofit.create(Isenond.class);//拿到接口代理实现类    Call<ResuteBean> list = isenond.getList("cook",0, 1, 20);//拿到call接口对象    //Call<ResuteBean> list = isenond.getList("top",0, 1, 20);          //Response<ResuteBean> execute = list.execute();//同步请求 在子线程中请求最好    list.enqueue(new Callback<ResuteBean>() {//加入异步队列        @Override        public void onResponse(Call<ResuteBean> call, Response<ResuteBean> response) { //String string = response.errorBody().string();//即使错误也可以获取到返回的内容            List<ResuteBean.TngouBean> tngou = response.body().getTngou();            adapter.addlist(tngou);        }        @Override        public void onFailure(Call<ResuteBean> call, Throwable t) {            Toast.makeText(seconActivity.this, "加载数据失败",    Toast.LENGTH_SHORT).show();        }    });}}//如果对获取的数据再次进行处理的话可以配合Rxjava使用将接口文件增加返回类型为Observablepublic interface IsFour {@GET("/api/{categroe}/list")Observable<ResuteBean> getList(@Path("categroe") String categroe, @Query("id") int id, @Query("name") int name, @Query("row") int row); }//第二步 Retrofit retrofit = new Retrofit.Builder()            .baseUrl("http://www.tngou.net")            .addConverterFactory(GsonConverterFactory.create())            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())            //为RxJava准备的CallAdapter,它将作为Observable返回。            .build();    IsFour four = retrofit.create(IsFour.class);    four.getList("cook", 0, 1, 20)            .observeOn(AndroidSchedulers.mainThread())            //.map()可以再次处理数据            .subscribe(new Observer<ResuteBean>() {                @Override                public void onSubscribe(Disposable d) {                }                @Override                public void onNext(ResuteBean resuteBean) {                    adapter.addlist(resuteBean.getTngou());//拿到处理的数据显示                }                @Override                public void onError(Throwable e) {                }                @Override                public void onComplete() {                }            });}
0 0