Home > Java > javaTutorial > java URL gets PHP JSON data

java URL gets PHP JSON data

高洛峰
Release: 2017-01-05 17:09:58
Original
1150 people have browsed it

1:php address http://127.0.0.6/?c=json
2:java The output result is

[{"id":1,"name":"zhdc" },{"id":2,"name":"\u5c0f\u6731"}]

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));
  }
}
Copy after login
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();
    }
  }
}
Copy after login


More java URL For articles related to obtaining PHP JSON data, please pay attention to the PHP Chinese website!


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Return JSON from PHP script
From 1970-01-01 08:00:00
0
0
0
How to convert json string to json object in php
From 1970-01-01 08:00:00
0
0
0
Extract and access JSON data using PHP
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template