(电报机器人)JSON post 请求无效

(Telegram bot) JSON post request doesn't work

我正在尝试向我发送的消息的接收者显示自定义键盘。

但是,我不知道为什么,但是当我尝试在 json 字符串中附加 reply_markup 参数时,以下代码无法正常工作。

我猜是因为我把reply_markup数组放错了,但找不到解决方法。

String query = "https://api.telegram.org/bot{token}/sendmessage";
String json = "{\"chat_id\":188784029,\"text\":\"123123\",\"reply_markup\":{\"keyboard\":[['1','2'],['3','4']], \"one_time_keyboard\":true}}";

URL url = new URL(query);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");

OutputStream os = conn.getOutputStream();
os.write(json.getBytes("UTF-8"));
os.close();

// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");


in.close();
conn.disconnect();

您的 JSON 无效,您应该使用 " 而不是 '

['1','2'],['3','4']

应替换为:

["1","2"],["3","4"]