java如何调用php接口,并获取值

时间:2024-03-17 18:09:16

URL url = new URL("php接口路径");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(10000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.setDoOutput(true);
 
 
    // 输出返回结果
    InputStream input = conn.getInputStream();
    int resLen =0;
    byte[] res = new byte[1024];
    StringBuilder sb=new StringBuilder();
    while((resLen=input.read(res))!=-1){
      sb.append(new String(res, 0, resLen));
    }
     
    String jsonStr=sb.toString();

 System.out.println(jsonStr);