Retrofit的使用与基本的封装

首先是导入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()方法,在这个方法中发起请求操作,通过callbackadapter()完成请求,请求到的json数据通过parsejson()方法交由子类重写,同时在parsejson()(即请求成功)方法中进行json解析,并将最终的解析结果直接发送给对应的Adapter中去进行填充数据.

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值