Becky Becky - 4 months ago 68
JSON Question

Getting Error While Inserting JSON object into JSOUP

I was trying to post some json objects into Saiku Server using JSOUP. Here is my code.

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


and I am getting error like

Errorjava.lang.IllegalArgumentException: Must supply an even number of key value pairs .


I searched in many sites but can't able to find the solution for that . Can someone clarify me . Thanks in Advance. i attached my code here,

Answer

I use the requestBody() for posting the JSON object.The requestBody() method is only available in the JSOUP 1.9.1 jar and i posted the code below for your reference.

    // 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());
Comments