我的应用程序以这样的方式发送POST请求:
URL url = new URL(SERVER_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.connect();
// Write
OutputStream output = urlConnection.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
writer.write(jsonString);
writer.close();
output.close();
urlConnection.disconnect();
但API无法对其进行解码,因为它是一个字符串。所以:
$input = json_decode(file_get_contents('php://input'), true);
只返回一个空数组。
如何在请求正文中编写JSON?
答案 0 :(得分:0)
也许你需要解析你的jsonString
。所以你的代码应该是:
writer.write(JSON.parse(jsonString));