使用POST客户端的java JSON字符串
大家好,我正在尝试从客户端捕获json“post”数据并将其发送到其他客户端,即我正在尝试获取点击我的url的客户端的数据,我处理这些数据并将其发送到实际的url。为此,我创建了一个内部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中
但是当我尝试放置它时,我得到了错误insert missing quote
请帮我解决这个问题
多谢各位
# 1 楼答案
移动评论以回答问题,因为它对OP有效
您可以将字符串转换为字节数组,如下所示: