1 private static RequestConfig requestConfig = null; 2 3 //get方式 4 public static JSONObject httpGet(String url){ 5 JSONObject jsonResult = null; 6 CloseableHttpClient client = HttpClients.createDefault(); 7 HttpGet request = new HttpGet(url); 8 request.setConfig(requestConfig); 9 try { 10 CloseableHttpResponse response = client.execute(request); 11 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ 12 HttpEntity entity = response.getEntity(); 13 String strResult = EntityUtils.toString(entity, "utf-8"); 14 jsonResult = JSONObject.parseObject(strResult); 15 }else{ 16 17 } 18 }catch (IOException e){ 19 20 }finally { 21 request.releaseConnection(); 22 } 23 return jsonResult; 24 }
通过传入url,可以解析路径返回的json参数