ホームページ  >  記事  >  Java  >  Java URLはPHP JSONデータを取得します

Java URLはPHP JSONデータを取得します

高洛峰
高洛峰オリジナル
2017-01-05 17:09:581051ブラウズ

1:phpアドレス http://127.0.0.6/?c=json
2:java 出力結果は

[{"id":1,"name":"zhdc"},{"id":2) ,"name":"u5c0fu6731"}]

index.php

<?php
if(isset($_REQUEST[&#39;c&#39;])){
  $c = $_REQUEST[&#39;c&#39;];
  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();
    }
  }
}


PHP JSON データを取得するためのその他の Java URL については、PHP 中国語 Web サイトの関連記事に注目してください。


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。