java URL 获取PHP JSON 数据
发布时间:2020-11-17 02:16:25 所属栏目:Java 来源:互联网
导读:1:php地址http://127.0.0.6/c=json2:java输出的结果是[{"id":1,"name":"zhdc"},{"id":2,"name":"u5c0fu6731"}]
1:php地址 http://127.0.0.6/?c=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(); } } } (编辑:甘南站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |