java URL 獲取PHP JSON 數(shù)據(jù)
更新時(shí)間:2016年04月29日 15:06:04 投稿:wulei
這篇文章主要介紹了java URL 獲取PHP JSON 數(shù)據(jù),需要的朋友可以參考下
1:php地址 http://127.0.0.6/?c=json
2:java 輸出的結(jié)果是
[{"id":1,"name":"zhdc"},{"id":2,"name":"\u5c0f\u6731"}]
2:java 輸出的結(jié)果是
[{"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();
}
}
}
相關(guān)文章
Spring\SpringBoot配置連接數(shù)據(jù)庫(kù)的方法
最近在學(xué)習(xí)SpringBoot,第一步就是要配置數(shù)據(jù)庫(kù),本文詳細(xì)的介紹了Spring\SpringBoot配置連接數(shù)據(jù)庫(kù)的方法,有需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
spring boot 1.5.4 集成shiro+cas,實(shí)現(xiàn)單點(diǎn)登錄和權(quán)限控制
這篇文章主要介紹了spring boot 1.5.4 集成shiro+cas,實(shí)現(xiàn)單點(diǎn)登錄和權(quán)限控制,需要的朋友可以參考下2017-06-06
Springboot集成阿里云OSS上傳文件系統(tǒng)教程
這篇文章主要介紹了Springboot集成阿里云OSS上傳文件系統(tǒng)教程,通過(guò)詳細(xì)的圖文展示,代碼步驟的展示和文件配置信息,希望對(duì)你有所幫助2021-06-06
Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用
這篇文章主要介紹了Spring Boot 2.x基礎(chǔ)教程之配置元數(shù)據(jù)的應(yīng)用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
spring AOP的After增強(qiáng)實(shí)現(xiàn)方法實(shí)例分析
這篇文章主要介紹了spring AOP的After增強(qiáng)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了spring面向切面AOP的After增強(qiáng)實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
IDEA中設(shè)置代碼自動(dòng)提示為Alt+/的具體做法
很多公司都強(qiáng)制性要求使用Intellij?IDEA,其實(shí)Intellij?IDEA也確實(shí)很好用,但是一下子從Eclipse跳轉(zhuǎn)到Intellij?IDEA轉(zhuǎn)也是需要一段時(shí)間的,為了迎合之前的習(xí)慣,就需要在Intellij?IDEA中改變一些設(shè)置,如代碼自動(dòng)生成,本文給大家分享設(shè)置方法,感興趣的朋友一起看看吧2023-01-01

