java URL 獲取PHP JSON 數(shù)據(jù)
更新時間:2016年04月29日 15:06:04 投稿:wulei
這篇文章主要介紹了java URL 獲取PHP JSON 數(shù)據(jù),需要的朋友可以參考下
1:php地址 http://127.0.0.6/?c=json
2:java 輸出的結果是
[{"id":1,"name":"zhdc"},{"id":2,"name":"\u5c0f\u6731"}]
2:java 輸出的結果是
[{"id":1,"name":"zhdc"},{"id":2,"name":"\u5c0f\u6731"}]
index.php
<?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();
}
}
}
相關文章
Spring\SpringBoot配置連接數(shù)據(jù)庫的方法
最近在學習SpringBoot,第一步就是要配置數(shù)據(jù)庫,本文詳細的介紹了Spring\SpringBoot配置連接數(shù)據(jù)庫的方法,有需要的朋友們下面隨著小編來一起學習學習吧2021-06-06
spring boot 1.5.4 集成shiro+cas,實現(xiàn)單點登錄和權限控制
這篇文章主要介紹了spring boot 1.5.4 集成shiro+cas,實現(xiàn)單點登錄和權限控制,需要的朋友可以參考下2017-06-06
Springboot集成阿里云OSS上傳文件系統(tǒng)教程
這篇文章主要介紹了Springboot集成阿里云OSS上傳文件系統(tǒng)教程,通過詳細的圖文展示,代碼步驟的展示和文件配置信息,希望對你有所幫助2021-06-06
Spring Boot 2.x基礎教程之配置元數(shù)據(jù)的應用
這篇文章主要介紹了Spring Boot 2.x基礎教程之配置元數(shù)據(jù)的應用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
spring AOP的After增強實現(xiàn)方法實例分析
這篇文章主要介紹了spring AOP的After增強實現(xiàn)方法,結合實例形式分析了spring面向切面AOP的After增強實現(xiàn)步驟與相關操作技巧,需要的朋友可以參考下2020-01-01

