retrofit(实现java访问外部接口)

我们在开发时候,程序中有时候可能需要访问已经提供的外部接口,这里使用retrofit访问。具体代码已经上传到github和gitlab。

这里是controller层调用例子。

@Role(roles = {RoleEnum.SUPER_ADMIN, RoleEnum.ADMIN, RoleEnum.OPERATOR})
@RequestMapping(value = "lockAction_CancelUnlockAuthority", method = RequestMethod.GET)
public Map<String, Object> CancelUnlockAuthority(@RequestParam Integer userId,
                                                 @RequestParam String userPhone,
                                                 @RequestParam Integer hotelId,
                                                 @RequestParam Integer buildingId,
                                                 @RequestParam Integer roomId) {
    LockInfo lockInfo = new LockInfo(hotelId.toString(), buildingId.toString(), roomId.toString());
    UserInfo userInfo = new UserInfo(userPhone, userId.toString(), lockInfo);
    Call<Map<String, Object>> mapCall = this.lockService.CancelUnlockAuthority(userInfo);
    return this.execute(mapCall);
}

private Map<String, Object> execute(Call<Map<String, Object>> unlock) {
    Response<Map<String, Object>> execute = null;
    try {
        execute = unlock.execute();
    } catch (IOException e) {
        throw new ProjectException(ExceptionCode.LOCK_SERVER_ERROR.getCode(), ExceptionCode.LOCK_SERVER_ERROR.getMessage());
    }
    Map<String, Object> body = execute.body();
    if (body == null) {
        throw new ProjectException(ExceptionCode.LOCK_SERVER_ERROR.getCode(), ExceptionCode.LOCK_SERVER_ERROR.getMessage());
    }
    Object isError = body.get("isError");
    if (isError != null && isError instanceof Boolean && !((Boolean) isError).booleanValue()) {
        return ResponseMap.success("success").data("data", body.get("data")).result();
    } else {
        throw new ProjectException(ExceptionCode.LOCK_SERVER_ERROR.getCode(), body.get("errorMessage") == null ? ExceptionCode.LOCK_SERVER_ERROR.getMessage() : body.get("errorMessage").toString());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值