我试图使用JSOUP将一些json对象发布到Saiku服务器中。这是我的代码。
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 .
我搜索了许多网站,但找不到解决方案。有人可以澄清我.提前谢谢。我在这里附上了我的代码,
我使用 requestBody() 来发布 JSON 对象。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());
根据javadoc:
Connection data(String... keyvals)
添加多个请求数据参数。可以一次设置多个参数,例如:.data("name","jsoup","language","Java","language","English");创建一个查询字符串,如下所示:?name=jsoup&language=Java&language=English
我认为你需要:
Connection requestBody(String body)
设置开机自检(或放置)请求正文。当服务器需要纯请求正文而不是 URL 编码表单键/值对的集合时很有用