【问题标题】:HTTP POST request with JSON object as data以 JSON 对象为数据的 HTTP POST 请求
【发布时间】:2014-10-13 16:46:38
【问题描述】:

我正在努力使用 JSON 对象作为数据发出 HTTP POST 请求。

如下所示,首先我创建了一个 HTTP Post 请求。然后我注释掉了它的一部分并尝试修改它以添加 JSON 相关代码。让我感到困惑的一件事是,尽管看到了许多使用导入“org.json.simple.JSONObject”的教程,但我的 IDE 读取了一条错误消息并指出“无法解析导入 org.json.simple.JSONObject”。

任何有关如何使此代码工作的建议将不胜感激。

 import java.io.*;
 import java.net.*;
 import org.json.simple.JSONObject;

 public class HTTPPostRequestWithSocket {

    public void sendRequest(){

        try {

            JSONObject obj = new JSONObject();
            obj.put("instructorName", "Smith");
            obj.put("courseName", "Biology 101");
            obj.put("studentName1", "John Doe");
            obj.put("studentNumber", new Integer(100));
            obj.put("assignment1", "Test 1");
            obj.put("gradeAssignment1", new Double("95.3"));

           /*
           //Note that this code was taken out in order to attempt to send
           //the information in the form of JSON.
           String params = URLEncoder.encode("param1", "UTF-8")
                + "=" + URLEncoder.encode("value1", "UTF-8");
            params += "&" + URLEncoder.encode("param2", "UTF-8")
                + "=" + URLEncoder.encode("value2", "UTF-8");
             */

            String hostname = "nameofthewebsite.com";
            int port = 80;

            InetAddress addr = InetAddress.getByName(hostname);
            Socket socket = new Socket(addr, port);
            String path = "/nameofapp";

            // Send headers
            BufferedWriter wr = new BufferedWriter(new     
            OutputStreamWriter(socket.getOutputStream(), "UTF8"));
            wr.write("POST "+path+" HTTP/1.0rn");
            wr.write("Content-Length: "+obj.length()+"rn");
            wr.write("Content-Type: application/x-www-form-urlencodedrn");
            wr.write("rn");

            // Send parameters
            wr.write(obj);
            wr.flush();

            // Get response
            BufferedReader rd = new BufferedReader(new   InputStreamReader(socket.getInputStream()));
             String line;

            while ((line = rd.readLine()) != null) {
                System.out.println(line);
                }

            wr.close();
            rd.close();
            socket.close();//Should this be closed at this point?
            }catch (Exception e) {e.printStackTrace();}
        }
}

【问题讨论】:

  • 是你的 CLASSPATH 中的 json-simple jar 文件吗?

标签: java json http post


【解决方案1】:

您的 IDE 说它无法解析导入 org.json.simple.JSONObject 的原因是因为 org.json.simple.* 包和类不包含在 Java 中,而是属于 JSON Simple library

【讨论】:

  • 谢谢。我必须下载并添加 jar。
  • 如果你能接受这个答案,那将有助于其他人也知道这个问题已经得到解答。谢谢。
  • 好的。我能做到。非常感谢您添加链接。但是,我仍然要重新发布这个问题,减去“import org.json.simple.JSONObject”部分,因为我真的很挣扎,希望有人能过来看看,足够亲切/仁慈在代码本身并帮助我让它工作。
【解决方案2】:

我认为使用 Socket 不是一个好主意。你可以更好地使用:

http://hc.apache.org/httpcomponents-client-ga/(一个 HTTP 客户端)

或 java.net.URLConnection。示例:

http://crunchify.com/create-very-simple-jersey-rest-service-and-send-json-data-from-java-client/

【讨论】:

    【解决方案3】:

    您需要带有 org.json.simple.JSONObject 实现的 jar: http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm

    【讨论】:

    • 我一直在努力查看来自不同教程的信息并将所有内容正确地综合在一起,以尝试使用 JSON 对象作为数据发出 HTTP POST 请求。那么,代码本身有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多