Retrofit简单封装
来源:互联网 发布:python 爬虫 视频教程 编辑:程序博客网 时间:2024/05/21 08:45
参考博客
简单实用步骤
1 ApiService,所有需要的接口
public interface ApiService { @GET("top250") Call<MovieResponse> getMovices(@Query("start") int start, @Query("count") int count);}
2 ApiManager,用来简单的封装Retrofit
public class ApiManager { /** * 基础地址 */ private static final String Base_Url = "https://api.douban.com/v2/movie/"; private static final Retrofit sRetrofit = new Retrofit.Builder() .baseUrl(Base_Url) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器 .build(); private static final ApiService apiManager = sRetrofit.create(ApiService.class); public static Call<MovieResponse> getMovies(int start, int end){ return apiManager.getMovices(start,end); }}
3 在activity中调用接口
public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getMovies(); } private void getMovies() { //参数的传递直接赋值?能不能用一个类进行封装? ApiManager.getMovies(0,10) .enqueue(new Callback<MovieResponse<List<Movie>>>() { @Override public void onResponse(Call<MovieResponse<List<Movie>>> call, Response<MovieResponse<List<Movie>>> response) { MovieResponse movieResponse = response.body(); List<Movie> list = (List<Movie>) movieResponse.getSubjects(); Log.d("shj--",list.toString()); setData(list); } @Override public void onFailure(Call<MovieResponse<List<Movie>>> call, Throwable t) { Log.d("shj--","error"+t.getMessage()); } }); } private void setData(List<Movie> list) { List<String> titles = new ArrayList<>(); for (int i=0;i<list.size();i++){ Movie m = list.get(i); titles.add(m.getTitle()); } BaseAdapter aa = new ArrayAdapter<String>(MainActivity.this, R.layout.main, titles); setListAdapter(aa); }}
思考:这里传递参数是直接赋值的,有没有统一的传参方式?
以上算是一个简单的网络请求了,但是考虑到有很多返回值,所以把返回值做一个统一的处理会好一点
/** * Created by apple on 17/7/6. * 0-请求成功 1-请求失败,登录过期 2-请求失败,无权限 3-请求失败-余额不足 */public abstract class HttpCallBack<T extends BaseCallModel> implements Callback<T> { @Override public void onFailure(Call<T> call, Throwable t) { } @Override public void onResponse(Call<T> call, Response<T> response) { if (response.raw().code() == 200) {//200是服务器有合理响应 if(response.body().errno == 0){ onSuc(response); } else if (response.body().errno == 1){ } else if (response.body().errno == 2){ onAutoLogin(); } else { onFail(response.body().msg); } } else {//失败响应 onFailure(call, new RuntimeException("response error,detail = " + response.raw().toString())); } } public abstract void onSuc(Response<T> response); public abstract void onFail(String message); public abstract void onAutoLogin();}
自定义Callback改成以上之后,网络请求变成
public class MainActivity2 extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getMovies(); } private void getMovies() { //不能进行测试,这个json没有错误码,但是思路是这样的,封装起来统一去处理 ApiManager.getMovies(0,10) .enqueue(new HttpCallBack<MovieResponse<List<Movie>>>() { @Override public void onSuc(Response<MovieResponse<List<Movie>>> response) { setData(response.body().getSubjects()); } @Override public void onFail(String message) { } @Override public void onAutoLogin() { } }); } private void setData(List<Movie> list) { List<String> titles = new ArrayList<>(); for (int i=0;i<list.size();i++){ Movie m = list.get(i); titles.add(m.getTitle()); } BaseAdapter aa = new ArrayAdapter<String>(MainActivity2.this, R.layout.main, titles); setListAdapter(aa); }}
下载链接,retrofit包下是该文章代码
阅读全文
1 0
- Retrofit简单封装
- Retrofit的简单封装
- Retrofit的简单封装
- Retrofit+RxJava 简单封装
- Retrofit简单封装
- Retrofit的简单封装
- Rxjava+Retrofit简单封装
- retrofit的简单二次封装
- Retrofit的简单封装使用
- Retrofit网络通信库简单封装使用
- RXJava+Retrofit+MVP的简单封装
- Rxjava2.0和Retrofit简单封装使用
- retrofit网络请求的简单封装使用
- Retrofit 学习笔记和简单封装
- Retrofit简单封装,实现链式编程
- 简单的Retrofit+Rxjava二次封装
- Retrofit和OkHttp简单的二次封装
- Retrofit -> 封装
- 常见DOS操作命令
- GreenDao学习以及基本使用(老罗学习总结)
- CodeForces 25D Roads not only in Berland
- 王爽.汇编.第三版.检测点17.1.疑似答案
- 孤单数
- Retrofit简单封装
- python学习日志--day6
- JAVA中使用ASN.1
- Ubuntu下的一些命令整理+Windoms下MySql命令+javaWeb服务器搭建+Ubuntu下JDK安装
- 【Centos7笔记二】常用命令
- 2Linux下的socket介绍
- C++11新特性--线程库相关
- 小甲鱼课程学习016-027
- 移动端300ms的点击延迟以及解决方案