public class Util { String str = ""; public String getjson(String strjson){ URL url=null; HttpURLConnection urlConnection=null; try { url=new URL(strjson); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); int res = urlConnection.getResponseCode(); if(res==200){ InputStream in = urlConnection.getInputStream(); byte[] b=new byte[1024]; int len=0; while((len=in.read(b))!=-1){ String st=new String(b,0,len); str+=st; } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return str; } }
使用HttpUUrlConnection网络请求
最新推荐文章于 2025-01-22 09:09:12 发布
本文介绍了一个简单的Java工具类,用于发起HTTP请求并获取JSON响应。该工具类使用了标准的Java库,包括URL和HttpURLConnection,实现了设置超时时间、读取响应等功能。
1519

被折叠的 条评论
为什么被折叠?



