HTTP 错误代码 415 - JAVA 中的 JSON 个数组
HTTP Error Code 415 - JSON arrays in JAVA
我正在尝试通过 JAVA 生成和发送一个 JSON 文件,当我试图添加一个带有数组的嵌套对象以适应应用程序的协议时(这并不重要)问题)java 程序无法发送文件,因为 HTTP 错误,代码 415(不支持的媒体类型),这很奇怪,因为当我在目标应用程序中复制它时,生成的 JSON 有效( Google 的 DialogFlow)。换句话说,JSON 可以正常工作,但 JAVA(1.8 版)无法识别它。有谁知道为什么会这样吗?
当带有 JSON 数组的部分未包含在 JSON 文件中时,请求发送没有问题(请参见下面的代码)。我尝试将内容类型从 "application/json;charset=utf8" 更改为 "application/json;charset=utf-8" 或 "application/json" 但没有任何效果(这部分未包含在代码中,因为导致 JSON 的更改不工作在下面的街区)。
部分不工作:
static JSONObject messageToJSON()
{
JSONObject requestJson = new JSONObject();
JSONObject meta= new JSONObject();
JSONObject payload= new JSONObject();
JSONArray arrayJson = new JSONArray();
String messageData="My Message";
try
{
requestJson.put("message", messageData);
requestJson.put("messageType", "html");
payload.put("title", "Thanks");
payload.put("message", "Thank you");
arrayJson.put(payload);
meta.put("contentType", "300");
meta.put("templateId", "6");
meta.put("contentType", "300");
meta.put("payload", arrayJson);
requestJson.put("metadata", meta);
System.out.println(requestJson.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return requestJson;
}
工作部分(没有 JSON 中的额外层,例如有效负载 JSON 对象和 arrayJson JSON 数组):
static JSONObject messageToJSON()
{
JSONObject requestJson = new JSONObject();
JSONObject meta= new JSONObject();
JSONObject payload= new JSONObject();
JSONArray arrayJson = new JSONArray();
String messageData="My Message";
try
{
requestJson.put("message", messageData);
requestJson.put("messageType", "html");
meta.put("contentType", "300");
meta.put("templateId", "6");
meta.put("contentType", "300");
requestJson.put("metadata", meta);
System.out.println(requestJson.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return requestJson;
}
好的,我找到问题了。我应该将 arrayJson 从 JSONArray 类型转换为字符串。
换句话说,而不是
meta.put("payload", arrayJson);
我应该
meta.put("payload", arrayJson.toString());
或者把arrayJson从头做成字符串格式
我正在尝试通过 JAVA 生成和发送一个 JSON 文件,当我试图添加一个带有数组的嵌套对象以适应应用程序的协议时(这并不重要)问题)java 程序无法发送文件,因为 HTTP 错误,代码 415(不支持的媒体类型),这很奇怪,因为当我在目标应用程序中复制它时,生成的 JSON 有效( Google 的 DialogFlow)。换句话说,JSON 可以正常工作,但 JAVA(1.8 版)无法识别它。有谁知道为什么会这样吗?
当带有 JSON 数组的部分未包含在 JSON 文件中时,请求发送没有问题(请参见下面的代码)。我尝试将内容类型从 "application/json;charset=utf8" 更改为 "application/json;charset=utf-8" 或 "application/json" 但没有任何效果(这部分未包含在代码中,因为导致 JSON 的更改不工作在下面的街区)。
部分不工作:
static JSONObject messageToJSON()
{
JSONObject requestJson = new JSONObject();
JSONObject meta= new JSONObject();
JSONObject payload= new JSONObject();
JSONArray arrayJson = new JSONArray();
String messageData="My Message";
try
{
requestJson.put("message", messageData);
requestJson.put("messageType", "html");
payload.put("title", "Thanks");
payload.put("message", "Thank you");
arrayJson.put(payload);
meta.put("contentType", "300");
meta.put("templateId", "6");
meta.put("contentType", "300");
meta.put("payload", arrayJson);
requestJson.put("metadata", meta);
System.out.println(requestJson.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return requestJson;
}
工作部分(没有 JSON 中的额外层,例如有效负载 JSON 对象和 arrayJson JSON 数组):
static JSONObject messageToJSON()
{
JSONObject requestJson = new JSONObject();
JSONObject meta= new JSONObject();
JSONObject payload= new JSONObject();
JSONArray arrayJson = new JSONArray();
String messageData="My Message";
try
{
requestJson.put("message", messageData);
requestJson.put("messageType", "html");
meta.put("contentType", "300");
meta.put("templateId", "6");
meta.put("contentType", "300");
requestJson.put("metadata", meta);
System.out.println(requestJson.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
return requestJson;
}
好的,我找到问题了。我应该将 arrayJson 从 JSONArray 类型转换为字符串。 换句话说,而不是
meta.put("payload", arrayJson);
我应该
meta.put("payload", arrayJson.toString());
或者把arrayJson从头做成字符串格式