[英]How to build the body of a request in json format
Here is the request I should call: listing library contents这是我应该调用的请求: 列出库内容
GET https://photoslibrary.googleapis.com/v1/mediaItems Content-type: application/json Authorization: Bearer oauth2-token { "pageSize": "100", }
GET https://photoslibrary.googleapis.com/v1/mediaItems内容类型:application/json 授权:Bearer oauth2-token { "pageSize": "100", }
Here's what I tried:这是我尝试过的:
public String getJSON(String url, int timeout) {
String body1 = "{pageSize: 100,}";
String body2 = "{\"pageSize\": \"100\",}";
HttpURLConnection request = null;
try {
URL u = new URL(url);
request = (HttpURLConnection) u.openConnection();
request.setRequestMethod("GET");
request.setRequestProperty("Authorization", "Bearer " + token);
request.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
request.setUseCaches(false);
request.setAllowUserInteraction(false);
request.setConnectTimeout(timeout);
request.setReadTimeout(timeout);
request.setRequestProperty("Content-Length", String.format(Locale.ENGLISH, "%d", body2.getBytes().length));
OutputStream outputStream = request.getOutputStream();
outputStream.write(body2.getBytes());
outputStream.close();
request.connect();
int status = request.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (request != null) {
try {
request.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return null;
}
If I use the GET method I get an error:如果我使用 GET 方法,我会收到错误消息:
java.net.ProtocolException: method does not support a request body: GET
java.net.ProtocolException:方法不支持请求正文:GET
I tried with POST but without success.我尝试使用 POST 但没有成功。
Thanks for your help.谢谢你的帮助。
I think you need to send a param along with this request我认为您需要与此请求一起发送参数
You can try this request in postman using this query您可以使用此查询在 postman 中尝试此请求
https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100 https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100
and pass the token in the authentication section.并在身份验证部分传递令牌。
Here you can do something like -在这里你可以做类似的事情 -
eg
URI uri = new URIBuilder("https://photoslibrary.googleapis.com/v1/mediaItems")
.addParameter("pageSize", 100)
.build();
Suggestion - Use retrofit for network requests and jackson-databind project for JSON conversions.建议 - 使用 retrofit 进行网络请求,使用 jackson-databind 项目进行 JSON 转换。
I think the documentation is wrong and you're supposed to use a POST request instead of GET.我认为文档是错误的,您应该使用 POST 请求而不是 GET。
Also there is an error in the documented JSON request body: there is a trailing comma that should not be there.记录在案的 JSON 请求正文中也存在错误:尾随逗号不应存在。 Use the following:
使用以下内容:
String body2 = "{\"pageSize\": \"100\"}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.