JAVA请求url并获取返回数据

方法:

    /**
	 * 请求的url链接  返回的是json字符串
	 * @param urlStr
	 * @return
	 */
	public static String getURLContent(String urlStr) {               
		//请求的url 
		URL url = null;      
	    //请求的输入流
	    BufferedReader in = null;   
		//输入流的缓冲
	    StringBuffer sb = new StringBuffer(); 
		try{
			url = new URL(urlStr);     
			in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8") ); 
			String str = null;  
			//一行一行进行读入
			while((str = in.readLine()) != null) {
				sb.append( str );     
			}     
		} catch (Exception ex) {   
			            
		} finally{    
			try{
				if(in!=null) {
					in.close(); //关闭流    
			    }     
		    }catch(IOException ex) {      
			        
		    }     
		}     
	   String result =sb.toString();     
	   return result;    
	}

使用方法:

String string = StringUtil.getURLContent("http://.....");
//转换成Record
//Record data = new Record().setColumns(FastJson.getJson().parse(string, Map.class)); 

猜你喜欢

转载自blog.csdn.net/qq_28256783/article/details/82346208