2016-07-06 6 views
1

私はJSOUPを使っていくつかのjsonオブジェクトをSaikuサーバに投稿しようとしていました。ここに私のコードです。エラーを取得中JSOUPにJSONオブジェクトを挿入中

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によると、ここ

答えて

1

Iは、(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()); 
+0

Thanks Buddy、It Works。 –

1

を自分のコードを添付:

Connection data(String... keyvals) 

要求データパラメータの数を追加します。複数のパラメータを一度に設定することができます(例:.data( "name"、 "jsoup"、 "language"、 "Java"、 "language"、 "English")。私はあなたが必要だと思う英語

=名= jsoup &言語= Javaの&言語:?のようなクエリ文字列を作成します

Connection requestBody(String body) 

がPOSTを設定します(またはPUT)リクエストボディ。有用サーバがURLの無地リクエストボディはなく、セットが符号化された形式のキー/値のペアを期待

関連する問題