使用JSON对象作为数据的HTTP POST请求 [英] HTTP POST request with JSON object as data

查看:1358
本文介绍了使用JSON对象作为数据的HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am struggling to make an HTTP POST request with JSON object as data.

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

As you can see below, first I created an HTTP Post request. Then I commented out part of it and attempted to modify it in order to add JSON related code. One of the things that confused me was that despite seeing a number of tutorials using the import "org.json.simple.JSONObject" my IDE reads an error message and states "the import org.json.simple.JSONObject cannot be resolved".

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

Any advice about how to make this code work would be much appreciated.

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


推荐答案

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

The reason your IDE says it cannot resolve the import org.json.simple.JSONObject is because the org.json.simple.* packages and classes are not included in Java, but rather belong to the JSON Simple library.

这篇关于使用JSON对象作为数据的HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆