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

    xiaoxiao2021-03-26  20

    依赖

    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()); } }

    转载请注明原文地址: https://ju.6miu.com/read-661253.html

    最新回复(0)