如何将json数据从Java发布到php服务器?

时间:2018-12-22 09:57:43

标签: java json eclipse post httpurlconnection

使用发布请求将json数据从Java代码发送到服务器。

我正在尝试通过Java端使用HTTP-URL-connection在json中发送ID。

这是网址 http://xenzet.com/ds/getrec.php

,这是它将接收数据的格式。 {“ id”:null,“ date”:“ 22-Dec-18”}

我只需要将ID从Java端发送到服务器即可。

如果我正在接收id = 5 那么json应该是{“ id”:5,“ date”:“ 22-Dec-18”} 这是Java代码

    try
            {
                 java.net.URL urlForPostRequest = new java.net.URL("http://xenzet.com/ds/getrec.php?");
                 System.out.println("Instantiated new URL: " + urlForPostRequest);
                 Map<String ,Object> params = new LinkedHashMap<>();
                 params.put("id", resultant);
                 String readLine = null;
                HttpURLConnection conection = (HttpURLConnection) urlForPostRequest.openConnection();
                conection.setDoOutput(true);
                conection.setRequestMethod("POST");

    //                    conection.setRequestProperty("User-Agent", "Mozilla/5.0");
                OutputStream os = conection.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");    
                osw.write("Just Some Text");
                osw.flush();
                osw.close();
                os.close();  //don't forget to close the OutputStream
                conection.connect();

               String result1;
              BufferedInputStream bis = new BufferedInputStream(conection.getInputStream());
              ByteArrayOutputStream buf = new ByteArrayOutputStream();
              int result2 = bis.read();
              while(result2 != -1) {
                  buf.write((byte) result2);
                  result2 = bis.read();
              }
              result1 = buf.toString();
              System.out.println(result1);
            }
            catch(Exception ex)
            {
                // TODO Auto-generated catch block
            ex.printStackTrace();
            }

获取文件也不例外。

1 个答案:

答案 0 :(得分:0)

您的POST API正在接受 FORM数据,您需要发送id的表单数据。

考虑以下问题,

public class Xenzet {

    public static void main(String[] args) {
        try {
            URL urlForPostRequest = new URL("http://xenzet.com/ds/getrec.php");
            System.out.println("Instantiated new URL: " + urlForPostRequest);
            final long id = 12;
            HttpURLConnection conection = (HttpURLConnection) urlForPostRequest.openConnection();
            conection.setDoOutput(true);
            conection.setRequestMethod("POST");
            conection.setRequestProperty("User-Agent", "Mozilla/5.0");
            conection.getOutputStream().write(("id="+id).getBytes(StandardCharsets.UTF_8));
            conection.connect();

            BufferedInputStream bis = new BufferedInputStream(conection.getInputStream());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int resultBuffer = bis.read();
            while (resultBuffer != -1) {
                bos.write((byte) resultBuffer);
                resultBuffer = bis.read();
            }
            String result = bos.toString();
            System.out.println(result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

输出:

Instantiated new URL: http://xenzet.com/ds/getrec.php
{"id":"12","date":"22-Dec-18"}