>  기사  >  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();
    }
  }
}


자세한 내용 Java URL에서 PHP JSON 데이터를 가져오는 것과 관련된 기사는 PHP 중국어 웹사이트에 주목하세요!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.