Retrofit的使用与基本的封装

来源:互联网 发布:绿色建筑设计软件 编辑:程序博客网 时间:2024/05/16 03:12

首先是导入Retrofit与Gson的依赖:

compile 'com.squareup.retrofit2:retrofit:2.1.0'compile 'com.squareup.retrofit2:converter-gson:2.1.0'

封装的代码是:
public abstract class BasePresenter {    private HashMap<String,String> errormap =  new HashMap<>();    public ResponseInfoAPI responseInfoAPI;    public BasePresenter() {        /*指定网络请求的主机名*/        Retrofit retrofit = new Retrofit.Builder().                baseUrl(Constant.BASEURL).                addConverterFactory(GsonConverterFactory.create()).                build();        //创建具体的网络请求实体对象        responseInfoAPI = retrofit.create(ResponseInfoAPI.class);        errormap.put("1","访问服务器失败");        errormap.put("2","服务器忙请稍后重试");        errormap.put("3","服务器没有更新数据,返回缓存数据");    }    class callbackadapter implements Callback<ResponseInfo>{        @Override        public void onResponse(Call<ResponseInfo> call, Response<ResponseInfo> response) {            /*成功*/            ResponseInfo body = response.body();            if (body.getCode().equals("0")){                String json = body.getData();                parsejson(json);            }else {                onFailure(call,new RuntimeException(errormap.get(body.getCode())));            }        }        @Override        public void onFailure(Call<ResponseInfo> call, Throwable t) {            /*失败*/            if (t instanceof RuntimeException){                showerror(t.getMessage());            }else {                showerror("网络连接失败");            }        }    }    protected abstract void showerror(String message);    protected abstract void parsejson(String json);}
其中的Constant.BASEURL是主机的地址

ResponseInfoAPI.class是定义的请求接口,在接口中将参数声明例如:
public interface ResponseInfoAPI {    //不带参数的请求    @GET(Constant.HOME_URL)    Call<ResponseInfo> getHomeInfo();    //带参数的请求,通过注解声明请求方式以及uri地址,(它会进行自动拼接操作,不需要手动拼接)    @GET(Constant.LOGIN)    Call<ResponseInfo> getUserInfo(@Query("username") String username,@Query("password") String password,                                   @Query("type") int type);}

后面的是对成功与失败之后不同的情况作出的不同处理,作为抽象 由子类实现
到这里,已经对retrofit进行了二次封装,具体的使用过程如下
public class HomePresenter extends BasePresenter {    public static final String HomePresenter = "HomePresenter";    private HomeFragment mhomeFragment;    public HomePresenter(HomeFragment homeFragment) {        this.mhomeFragment = homeFragment;    }    @Override    protected void showerror(String message) {    }    @Override    protected void parsejson(String json) {        Gson gson = new Gson();        HomeInfo homeInfo = gson.fromJson(json, HomeInfo.class);        Log.i(HomePresenter,json);        mhomeFragment.getAdapter().setDate(homeInfo);    }    public void getHomeData(){        Call<ResponseInfo> homeInfo = responseInfoAPI.getHomeInfo();        homeInfo.enqueue(new callbackadapter());    }}

我们创建一个类去继承BasePresenter,实现父类当中的成功与失败方法,在activity或者fragment界面去创建子类对象调用其中自定义的getHomeData()方法,在这个方法中完成请求操作,同时在parsejson()(即请求成功)方法中进行json解析,并将最终的解析结果直接发送给对应的Adapter中去进行填充数据.

个人认为:retrofit的使用并不复杂,工作中更多使用的是OKhttp,,Volley我个人倒是不经常用,相比而言,两者之间各有各的好处,选择倾向因人而异