【发布时间】:2017-08-06 08:47:28
【问题描述】:
我是 web 服务的新手,我正在调用一个 web 服务,它应该返回带有以下代码的 JSON - 问题是我得到的是 xml 格式的响应 当我使用 google rest api 尝试相同的参数时 - 响应在 JSON 中 任何想法我做错了什么?
public static String getSFData(String urlSuffix) throws MalformedURLException, ProtocolException , IOException
{
String header = "Basic XXXXX";
URL url = new URL("https://api2.successfactors.eu/odata/v2/"+urlSuffix);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("authorization",header);
connection.setRequestProperty("Content-Type", "application/json");
BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bf.readLine()) != null )
{
stringBuffer.append(line);
}
String response = stringBuffer.toString();
System.out.println("response"+response);
return response;
}
【问题讨论】:
-
Content-Type指定您要发送的内容类型,并且您不发送任何内容(您也不应该使用GET)。要指定所需的 response 内容类型,请改为设置Accept标头值。 -
你能告诉我怎么做吗?我不明白
-
正如我所说,设置
Accept标头值而不是,即将"Content-Type"替换为"Accept"。 -
知道了!!我添加了 connection.setRequestProperty("Accept", "application/json");解决了我的问题 - 谢谢安德烈亚斯!!
标签: java json web-services