简单的retrofit使用方法

retrofit这个网络框架已经出来很长时间了, 现在相当的火爆, 下面来记录下我自己的使用情况!

首先我们使用android studio来操作:

第一步: 当然是引用retrofit的相关jar包

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

一般是这几个, 最主要的是第一个, 下面两个按需添加

第二步:

当然是要初始化retrofit

public Retrofit getRetrofit(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)//你的基础URL
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

   return retrofit;
}

第三步:

创建接口的service(每一个接口需要写一个)(此方法是以表单形式提交的)

public interface SDKGuestLoginService {
    @Multipart
    @POST("api/account/guestLogin")
    Call<ResponseBody> getData(@PartMap Map<String, RequestBody> map);
} 

第四步:

开始写公共方法

public Call<ResponseBody> SDKInit(LinkedHashMap<String, String> map) {
    SDKInitService service = RetrofitInit.getInstance().getRetrofit().create(SDKInitService.class);
    Call<ResponseBody> call = service.getData(getBody(map));
    return call;
}
private Map<String, RequestBody> getBody(LinkedHashMap<String, String> map) {
    Map<String, RequestBody> resultMap = new LinkedHashMap<>();
    String sign = LYLGameCommon.getLYLGameSign(map);//我自己的  可以删除

    if (TextUtils.isEmpty(sign)) {
        UtilLog.e("=========", "LYLGame error");
        return null;
    }

    map.put("_sign", sign);
    //下面是主要部分
    for (String key : map.keySet()) {
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), map.get(key) == null ? "" : map.get(key));
        UtilLog.i("=========", " 参数 ==> " + map.get(key));
        resultMap.put(key, body);
    }

    return resultMap;
}

第五步:

扫描二维码关注公众号,回复: 2629870 查看本文章

在其他地方调用SDKInit()这个方法

public void lYLGameSDKInit(String channelId, String gameId, String platformId, String version, final CallBackListener listener) {
    String deviceCode = LYLGameGetDeviceInfo.getDeviceCode();
    String deviceName = LYLGameGetDeviceInfo.getDeviceName();

    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    map.put("channel_id", channelId);
    map.put("device_code", deviceCode);
    map.put("device_name", deviceName);
    map.put("game_id", gameId);
    map.put("platform_id", platformId);
    map.put("version", version);
    //下面是主要方法
    Call<ResponseBody> call = HttpManager.getInstance().SDKInit(map);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            //服务端返回的内容在response里
            sendMessageToChannel(response, listener, 1);
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            listener.onFail(LYLGameSDKCode.SDK_INIT_FAILURE, LYLGameSDKCode.FAILURE);
        }
    });
}

以上就是最近使用的retrofit, 比较初级, 并没有像大牛那样进行各种封装, 下面是使用过程中的其他问题

1. 上面是以表单的形式提交, 那如果是application/json 格式提交呢?

首先service修改:

public interface SDKGuestLoginService {
    @POST("api/account/guestLogin")
    Call<ResponseBody> getData(@Body RequestBody body);//括号里是主要修改  删除@Multipart
}

getData()方法需要修改下

  private RequestBody getBody(HashMap<String,String> map){
        Gson gson = new Gson();
        String gsBody = gson.toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), gsBody);
        return body;
    }

大概就是这样了, 比较初级, 欢迎指正!

猜你喜欢

转载自blog.csdn.net/andcisco/article/details/81507528