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
- Rotrofit的简单使用
- 项目中对Rotrofit网络的简易封装
- 新手学习Rotrofit+RxJava网络请求以及Material Design风格
- 简单易懂的使用
- ant的简单使用
- Log4j的简单使用
- TCPDUMP的简单使用
- IComparer的简单使用
- TreeView的简单使用
- CListCtrl的简单使用
- Rose的简单使用
- OpenSSL的简单使用
- CInternetSession的简单使用
- 标签的简单使用
- 触发器的简单使用
- nekohtml的简单使用
- FileUpload的简单使用
- JFreeChart的简单使用
- C++const函数和const对象
- GMT单位及默认设置
- 一维数组写杨辉三角
- Linux的起源:从一个故事说起
- PHP array指定元素操作
- Rotrofit的简单使用
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- 系统时间,OSTimeGet()和OSTimeSet()
- 树莓派----03_安装QT5
- 关系运算符——三个数排序
- 一般图最大匹配 带花树算法 模板
- IntelliJ IDEA 创建 hello world Java web Maven项目从头到尾都有图有真相2017版本
- 题目1123:采药
- 安装phpredis插件出现的问题