1. 程式人生 > 其它 >java訪問url介面,返回json字串

java訪問url介面,返回json字串

 public static String loadJSON (String url) {        
     StringBuilder json = new StringBuilder();        
 try 
 {             
 URL oracle = new URL(url);            
 URLConnection yc = oracle.openConnection();
           
 BufferedReader in = new  BufferedReader(new InputStreamReader(yc.getInputStream()));
             
 String inputLine = null;           
 while 
 ( (inputLine = in.readLine()) !=  null ) {               
 json.append(inputLine);           
 }            
 in.close();
         
 } 
 catch(MalformedURLException e) {
          
 } 
 catch(IOException e) {        
 }        
 return json.toString();
 
     
 }
 
 }