2016-09-06 5 views
-1

こんにちはすべて私はクライアントからjson "投稿"データをキャプチャして他のクライアントに送信しようとしています。つまり、私のURLに当たったクライアントのデータを取得しようとしています。実際のURLに送信します。これは、私はデータを処理し、私の実際のURLに送信する内部のポストクライアントを作成しました。POSTクライアントを使用しているJSON文字列

URL url = new URL(urlPath); 
    conn = (HttpURLConnection) url.openConnection(); 
    conn.setRequestMethod("POST"); 
    conn.setDoOutput(true); 
    String a = book.getId(); 
    String b = book.getName(); 
    String c = book.getAuthor(); 
    String d = book.getPrice(); 

    System.out.println(a + b + c + d); 

    byte[] out = "{\"id\":\"root\",\"name\":\"password\",\"price\":\"root\",\"author\":\"password\"}" 
      .getBytes(); 
    int length = out.length; 

    conn.setFixedLengthStreamingMode(length); 
    conn.setRequestProperty("Content-Type", 
      "application/json; charset=UTF-8"); 
    conn.connect(); 
    OutputStream os = conn.getOutputStream(); 
    os.write(out); 
    BufferedReader br = new BufferedReader(new InputStreamReader(
      conn.getInputStream())); 
    while ((output = br.readLine()) != null) { 
     response = output; 
    } 
    // return parseJSON(response); 
    return response; 

文字列a、b、c、dの値をそれぞれルート、パスワード、ルート、パスワードで置きたいとします。

しかし、私はそれを配置しようとするとエラーが挿入見積もりを挿入します。

この

に関する私を助けてください

+0

は、あなたが入れてみてくださいでした '{\ "ID \ ""? '適切 ...これを試してみます" \ "\"、\ "名前\":\ "+" \ "価格\":\ " } " –

+0

はいこれを試してみましたが、文字列として取りますが、この出力を書き込むためにバイトが必要です – mark

+0

これを使って文字列からバイト配列を得ることができます 新しい文字列(" {\ "id \":\ " \ "+ \"、 "\"、\ "+ \"、\ "+ \"、 " \ "}")。getBytes() –

答えて

0

が、それはOPのために働いたとして回答にコメントを移動していただきありがとうございます。

あなたは以下のように文字列をバイト配列に変換することができます:\ "" +:

byte[] request = new String("{\"id\":\"" + a + "\",\"name\":\"" +b+"\",\"price\":\"" + c+" \",\"author\":\"" + d+ "\"}").getBytes() 
関連する問題