123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /*
- * *************************************************
- * Copyright (c) 2018 QMTH. All Rights Reserved.
- * Created by Deason on 2018-07-17 17:32:00.
- * *************************************************
- */
- package cn.com.qmth.examcloud.app.utils;
- import cn.com.qmth.examcloud.app.model.Constants;
- import cn.com.qmth.examcloud.app.model.ResBody;
- import cn.com.qmth.examcloud.app.model.Result;
- import okhttp3.Request;
- import okhttp3.Response;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class HttpUtils {
- private static Logger log = LoggerFactory.getLogger(HttpUtils.class);
- public static Result<String> call(Request request) throws Exception {
- Response response = HttpBuilder.client.getInstance().newCall(request).execute();
- String bodyStr = response.body().string();
- if (response.isSuccessful()) {
- return new Result().success(bodyStr);
- } else {
- log.warn("Http response is " + bodyStr);
- ResBody body = new JsonMapper().fromJson(bodyStr, ResBody.class);
- if (body != null && body.getCode() != null) {
- if (Constants.CODE_403.equals(body.getCode())) {
- return new Result().noAuthError();
- }
- if (Constants.CODE_404.equals(body.getCode())) {
- return new Result().noFoundError();
- }
- if (Constants.CODE_0.equals(body.getCode())) {
- return new Result().error(body.getErrorMsg());
- }
- return new Result().error(body.getDesc());
- }
- return new Result().error(bodyStr);
- }
- }
- }
|