将 JSON 对象插入 JSOUP 时出错
Getting Error While Inserting JSON object into JSOUP
我正在尝试使用 JSOUP post 一些 json 对象到 Saiku Server 中。这是我的代码。
Response document1 = Jsoup
.connect(
"http://localhost:8080/saiku/rest/saiku/admin/datasources/")
.header("Content-Type", "application/json")
.data(testJson.toJSONString())
.ignoreContentType(true)
.referrer("http://localhost:8080/")
.cookie("JSESSIONID", res.cookie("JSESSIONID"))
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36")
.method(Method.POST).timeout(10000).execute();
我收到类似
的错误
Errorjava.lang.IllegalArgumentException: Must supply an even number of key value pairs .
我在很多网站上进行了搜索,但找不到解决方案。有人可以澄清我。提前致谢。我在这里附上我的代码,
根据 javadoc:
Connection data(String... keyvals)
Add a number of request data parameters. Multiple parameters may be set at once, e.g.: .data("name", "jsoup", "language", "Java", "language", "English"); creates a query string like: ?name=jsoup&language=Java&language=English
我认为你需要:
Connection requestBody(String body)
Set a POST (or PUT) request body. Useful when a server expects a plain request body, not a set for URL encoded form key/value pairs
我使用 requestBody() 发布 JSON object.The requestBody() 方法仅在 JSOUP 1.9.1 jar 中可用,我发布了下面的代码供您参考。
// JSON Object
JSONObject testJson = new JSONObject();
testJson.put("connectionname", "drinkss");
testJson.put("jdbcurl", "jdbc:mysql://localhost/drink");
testJson.put("schema", "datasources/dr.xml");
testJson.put("driver", "com.mysql.jdbc.Driver");
testJson.put("username", "root");
testJson.put("password", "211218");
testJson.put("connectiontype", "MONDRIAN");
// For Posting datasource into server
Response document1 = Jsoup
.connect(
"http://localhost:8080/saiku/rest/saiku/admin/datasources/")
.header("Content-Type", "application/json")
.requestBody(testJson.toString())
.data(testJson)
.ignoreContentType(true)
.referrer("http://localhost:8080/")
.cookie("JSESSIONID", res.cookie("JSESSIONID"))
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36")
.method(Method.POST).timeout(10000).execute();
System.out.println("post successfully....."
+ testJson.toJSONString());
我正在尝试使用 JSOUP post 一些 json 对象到 Saiku Server 中。这是我的代码。
Response document1 = Jsoup
.connect(
"http://localhost:8080/saiku/rest/saiku/admin/datasources/")
.header("Content-Type", "application/json")
.data(testJson.toJSONString())
.ignoreContentType(true)
.referrer("http://localhost:8080/")
.cookie("JSESSIONID", res.cookie("JSESSIONID"))
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36")
.method(Method.POST).timeout(10000).execute();
我收到类似
的错误 Errorjava.lang.IllegalArgumentException: Must supply an even number of key value pairs .
我在很多网站上进行了搜索,但找不到解决方案。有人可以澄清我。提前致谢。我在这里附上我的代码,
根据 javadoc:
Connection data(String... keyvals)
Add a number of request data parameters. Multiple parameters may be set at once, e.g.: .data("name", "jsoup", "language", "Java", "language", "English"); creates a query string like: ?name=jsoup&language=Java&language=English
我认为你需要:
Connection requestBody(String body)
Set a POST (or PUT) request body. Useful when a server expects a plain request body, not a set for URL encoded form key/value pairs
我使用 requestBody() 发布 JSON object.The requestBody() 方法仅在 JSOUP 1.9.1 jar 中可用,我发布了下面的代码供您参考。
// JSON Object
JSONObject testJson = new JSONObject();
testJson.put("connectionname", "drinkss");
testJson.put("jdbcurl", "jdbc:mysql://localhost/drink");
testJson.put("schema", "datasources/dr.xml");
testJson.put("driver", "com.mysql.jdbc.Driver");
testJson.put("username", "root");
testJson.put("password", "211218");
testJson.put("connectiontype", "MONDRIAN");
// For Posting datasource into server
Response document1 = Jsoup
.connect(
"http://localhost:8080/saiku/rest/saiku/admin/datasources/")
.header("Content-Type", "application/json")
.requestBody(testJson.toString())
.data(testJson)
.ignoreContentType(true)
.referrer("http://localhost:8080/")
.cookie("JSESSIONID", res.cookie("JSESSIONID"))
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36")
.method(Method.POST).timeout(10000).execute();
System.out.println("post successfully....."
+ testJson.toJSONString());