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 輸出的結(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)文章
SpringBoot內(nèi)嵌tomcat處理有特殊字符轉(zhuǎn)義的問題
這篇文章主要介紹了SpringBoot內(nèi)嵌tomcat處理有特殊字符轉(zhuǎn)義的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
詳解Java弱引用(WeakReference)的理解與使用
這篇文章主要介紹了Java弱引用(WeakReference)的理解與使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
使用MyEclipse 開發(fā)struts2框架實現(xiàn)登錄功能(結(jié)構(gòu)教程)
這篇文章主要介紹了使用MyEclipse 開發(fā)struts2框架實現(xiàn)登錄功能(結(jié)構(gòu)教程)的相關(guān)資料,需要的朋友可以參考下2016-03-03
java foreach循環(huán)為什么不能賦值的講解
這篇文章主要介紹了java foreach循環(huán)為什么不能賦值的講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
使用logstash同步mysql數(shù)據(jù)到elasticsearch實現(xiàn)
這篇文章主要為大家介紹了使用logstash同步mysql數(shù)據(jù)到elasticsearch實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12

