大家好我想尝试从客户端捕获json“post”数据并将其发送给其他客户端,即我正在尝试获取打到我的URL的客户端的数据,我处理这些数据并将其发送到实际网址。为此我创建了一个内部帖子客户端,我处理数据并发送到我的实际网址。
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的值分别放在root,password,root,password中。
但是当我尝试放置它时,我得到错误插入缺少引号。
请帮我解决这个问题
谢谢
答案 0 :(得分:0)
将评论移至答案,因为它适用于OP。
您可以将字符串转换为字节数组,如下所示:
byte[] request = new String("{\"id\":\"" + a + "\",\"name\":\"" +b+"\",\"price\":\"" + c+" \",\"author\":\"" + d+ "\"}").getBytes()