| | |
| | | import com.google.gson.GsonBuilder; |
| | | import com.google.gson.JsonSyntaxException; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.Response4Http; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | |
| | | result = result.substring(0, result.lastIndexOf("&")); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 执行GET请求,传入Map参数和Url,获取响应 |
| | | */ |
| | | public static Response4Http doGet(String httpUrl, Map<String, ?> params) { |
| | | Response4Http responseResult = new Response4Http<>(); |
| | | |
| | | String queryParams = urlEncode(params); |
| | | Response response = HttpUtil.doGet(httpUrl, queryParams); |
| | | Integer httpResponseCode = response.getCode(); |
| | | String responseJson = (String) response.getData(); |
| | | if(httpResponseCode == 1) { //请求成功 |
| | | Response4Http responseHttp = HttpUtil.getGson().fromJson(responseJson, Response4Http.class); |
| | | if(responseHttp.getCode() == 0 ) { //接口返回状态码为成功 |
| | | //返回信息有 data属性 |
| | | if(responseHttp.getData()!=null) { |
| | | return responseResult.setII(1,true,responseHttp.getData(),null); |
| | | } |
| | | //返回信息有 status属性 |
| | | else if(responseHttp.getStatus() !=null){ |
| | | return responseResult.setStatus(1,true,responseHttp.getStatus()); |
| | | } |
| | | //返回信息有 result属性 |
| | | else if(responseHttp.getResult() !=null){ |
| | | responseResult.setResult(responseHttp.getResult()); |
| | | return responseResult.set(1,true); |
| | | } |
| | | else{ |
| | | return responseResult.set(1,true,"请求成功且返回数据正常,无数据信息"); |
| | | } |
| | | }else{ //接口返回状态码为失败 |
| | | // 返回信息有 msg属性 |
| | | return responseResult.set(1,false,responseHttp.getMsg()); |
| | | } |
| | | }else{ //请求失败 |
| | | return responseResult.set(0,response.getData(),response.getMsg()); |
| | | } |
| | | |
| | | } |
| | | } |