2016-09-06 23 views
-1

你好,我试图从客户端捕获json“post”数据并将它发送给其他客户端,即我试图获取打击我的url的客户端数据并处理这些数据并将其发送到实际的url。为此,我创建了一个内部post客户端,用于处理数据并发送到我的实际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 \”:\ “” + a +“\”,\“name \”:\“”+ b +“\”,\“price \”:\“”+ c +“\”,\“author \”:\“”+ d +“\” }“ –

+0

是的,当我试过这需要作为字符串,但我需要字节为了写这个输出 – mark

+0

你可以从字符串使用这个字节数组。 new String(”{\“id \”:\“ “+ a +”\“,\”name \“:\”“+ b +”\“,\”price \“:\”“+ c +”\“,\”author \“:\”“+ d +” \“}”)。getBytes() –

回答

0

移动评论回答,因为它为OP。

可以字符串转换为字节数组如下:

byte[] request = new String("{\"id\":\"" + a + "\",\"name\":\"" +b+"\",\"price\":\"" + c+" \",\"author\":\"" + d+ "\"}").getBytes() 
相关问题