retrofit网络请求的简单封装使用

依赖

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

BasePresenter  这个base类一般用来封装

package com.example.liuan.takeout.presenter.net;

import com.example.liuan.takeout.model.bean.ResponseInfo;
import com.example.liuan.takeout.presenter.ResponseInfoApi;
import com.example.liuan.takeout.utils.Constant;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Name: BasePresenter
 * Action:
 * Author: liuan
 * creatTime:2017-02-05 10:45
 */

public abstract class BasePresenter {

    public ResponseInfoApi responseInfoApi;
    private HashMap<String,String> errormap=new HashMap<>();


    public BasePresenter() {
        //1 初始化retrofit
        //
        Retrofit build = new Retrofit.Builder()
                .baseUrl(Constant.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //制定此对象需要发送的网络请求
        //完整的url地址
        responseInfoApi = build.create(ResponseInfoApi.class);


        errormap.put("1","没有最新数据");
        errormap.put("2","服务器");
        errormap.put("3","服务器异常");

}
    //同步 okhttp
    //异步  框架开启了子线程,回调方法 成功和失败
    class CallBackAdapter implements Callback<ResponseInfo>{

        @Override
        public void onResponse(Call<ResponseInfo> call, Response<ResponseInfo> response) {
            //请求成功
            //服务器返回数据的具体内容
            ResponseInfo body = response.body();
            String code = body.getCode();
            if(code.equals("0")){
                //请求成功
                parseJson(body.getData());


            }else{
                String errorMessage = errormap.get(code);
                //所有的异常都在onFailure中处理
        onFailure(call,new RuntimeException(errorMessage));

            }
        }

        @Override
        public void onFailure(Call<ResponseInfo> call, Throwable t) {
            if(t instanceof  RuntimeException){
            //请求失败
            String message = t.getMessage();
            showErrorMessage(message);
            }
            showErrorMessage("服务器忙,请稍后重试");
        }
    }

    /**
     *
     * @param json
     */
    protected abstract void parseJson(String json);

    /**
     *
     * @param message 错误的描述
     */

    protected abstract void showErrorMessage(String message) ;
}


ResponseInfoApi  这个 用注解的形式确定get请求或者post请求 请求参数

package com.example.liuan.takeout.presenter;

import com.example.liuan.takeout.model.bean.ResponseInfo;
import com.example.liuan.takeout.utils.Constant;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Name: ResponseInfoApi
 * Action:
 * Author: liuan
 * creatTime:2017-02-05 10:54
 */
public interface ResponseInfoApi {
    完整的url地址 get post请求方式 请求参数 接受请求结果(xml.参数)
//Call后面的T就代表网络请求发送成功后 获取到的javaBean

    @GET(Constant.HOME)
    Call<ResponseInfo>getHomeInfo(@Query("latitude") String lat, @Query("longitude") String lon);
}


HomePresenter 这个是具体实现 要看接口文档


package com.example.liuan.takeout.presenter.net;

import android.util.Log;

import com.example.liuan.takeout.model.bean.HomeInfo;
import com.example.liuan.takeout.model.bean.ResponseInfo;
import com.example.liuan.takeout.ui.adapter.HomeAdapter;
import com.google.gson.Gson;

import retrofit2.Call;

/**
 * Name: HomePresenter
 * Action:
 * Author: liuan
 * creatTime:2017-02-05 11:44
 */

public class HomePresenter extends BasePresenter {
    private static final String TAG = "HomePresenter";
    private final HomeAdapter homeAdapter;

    public HomePresenter(HomeAdapter homeAdapter) {
        this.homeAdapter=homeAdapter;
    }

    @Override
    protected void parseJson(String json) {
      Log.e(TAG, "parseJson: "+json);
        //json 解析
        Gson gson = new Gson();
        HomeInfo homeInfo = gson.fromJson(json, HomeInfo.class);
        //homeInfo中的数据需要展示在HomeFragment对应的RecyclerView的数据适配器中
        homeAdapter.setData(homeInfo);
    }

    @Override
    protected void showErrorMessage(String message) {

    }
    //发送请求方法
    public void getHomeData(String lat,String lon){
        //发送请求出去 连接诶地址 请求方式 请求参数由getHomeInfo指定

        Call<ResponseInfo> homeInfo = responseInfoApi.getHomeInfo(lat, lon);
        //请求发出去以后 在那个回调方法中处理 制定CallBackAdapter
        //处理CallBackAdapter 实现了CallBAck的接口 并且实现了onRes
        homeInfo.enqueue(new CallBackAdapter());


    }

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值