java 使用URLConnection 请求json格式接口,使用实体类接收

本文介绍了一个使用Java实现的通用GET请求发送方法,该方法能够动态替换URL中的参数值,并解析服务器返回的JSON结果为指定Java类对象。此外,还介绍了如何缓存JAXB上下文和字段以提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

parameter需要传递参数的参数对象,class用于接收json的对象,url接口的url地址 要替换参数值${}
 
public static String LOGOUTT = apiUrl+"/app/WebService/JSON/display.php/Logout?website=${website}&username=${username}&key=${key}";
public class CoreExecuteCc { private static final Logger logger = Logger.getLogger(CoreExecuteCc.class); final static ConcurrentMap<Class,JAXBContext> contextCache = new ConcurrentHashMap<Class,JAXBContext>(); final static ConcurrentMap<Class,Field[]> fieldsCache = new ConcurrentHashMap<Class,Field[]>(); public static <T> T sendGet(Object parameter,Class<T> clazz,String url,int type) { String result = ""; BufferedReader in = null; InputStreamReader inputStreamReader = null; InputStream inputStream = null; try { Field[] fields = fieldsCache.get(parameter.getClass()); if(fields == null){ fields = parameter.getClass().getFields(); fieldsCache.put(clazz,fields); } for (int i = 0; i < fields.length; i++) { Field filed = fields[i]; String filedName = filed.getName(); Object value = null; try { value = filed.get(parameter); } catch (IllegalAccessException e) { e.printStackTrace(); } if(value != null){ url = url.replace("${"+filedName+"}", value.toString()); } } url = url.replaceAll("&[^\\&]*=\\$\\{.*?\\}", ""); Pattern pattern = Pattern.compile("\\?.*?\\$\\{.*?\\}"); Matcher matcher = pattern.matcher(url); if(matcher.find()){ String matchUrl = matcher.group(0); url = url.replace(matchUrl.substring(1), ""); } URL realUrl = new URL(url.trim()); URLConnection connection = realUrl.openConnection(); connection.setConnectTimeout(20000); connection.setReadTimeout(20000); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); System.out.println("cc接口请求URL---"+url); Map<String, List<String>> map = connection.getHeaderFields(); for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); in = new BufferedReader(inputStreamReader); String inputLine = null; while ((inputLine = in.readLine()) != null) { in.close(); Gson gson = new Gson(); Map <String,Object> mapp = gson.fromJson(inputLine,Map.class); boolean returnFlag = Boolean.valueOf(mapp.get("result")+""); System.out.println("cc接口调用结果:"+returnFlag); if(returnFlag){ return gson.fromJson(inputLine, clazz); }else { mapp.put("result","false"); if(type==1){ mapp.remove("data"); mapp.put("baseData",baseData.getData()); } return gson.fromJson(new Gson().toJson(mapp), clazz); } } } catch (Exception e) { System.out.println("cc 请求URL异常" + e); e.printStackTrace(); } finally { try { if (in != null) { in.close(); } if(inputStreamReader!=null){ inputStreamReader.close(); } if(inputStream!=null){ inputStream.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return null; }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值