java URL 獲得PHP JSON 數據。本站提示廣大學習愛好者:(java URL 獲得PHP JSON 數據)文章只能為提供參考,不一定能成為您想要的結果。以下是java URL 獲得PHP JSON 數據正文
<?php if(isset($_REQUEST['c'])){ $c = $_REQUEST['c']; if($c == "json"){ $arr = array( array("id"=>1,"name"=>"zhdc"), array("id"=>2,"name"=>"小朱") ); die(json_encode($arr)); } }
Main.class import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Main { public static void main(String[] args){ try { URL url = new URL("http://127.0.0.6/?c=json"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.connect(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); Reader reader = new InputStreamReader(bufferedInputStream); String json = ""; int c; while((c = reader.read()) != -1){ json += (char)c; } System.out.println(json); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }