英文:
How to build the body of a request in json format
问题
以下是翻译好的内容:
这是我应该调用的请求:
列出库内容
>GET https://photoslibrary.googleapis.com/v1/mediaItems
Content-type: application/json
Authorization: Bearer oauth2-token
{
"pageSize": "100",
}
这是我尝试过的方式:
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;
	}
如果我使用GET方法,会出现错误:
>java.net.ProtocolException:该方法不支持请求体:GET
我尝试过使用POST,但没有成功。
感谢您的帮助。
英文:
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",
}
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:
>java.net.ProtocolException: method does not support a request body: GET
I tried with POST but without success.
Thanks for your help.
答案1
得分: 1
我认为您需要在此请求中发送一个参数
您可以尝试在Postman中使用以下查询进行此请求
https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100
并在身份验证部分传递令牌。
您可以像这样做 -
例如 
URI uri = new URIBuilder("https://photoslibrary.googleapis.com/v1/mediaItems")
.addParameter("pageSize", 100)
.build();
建议 - 使用retrofit进行网络请求,使用jackson-databind项目进行JSON转换。
英文:
I think you need to send a param along with this request
You can try this request in postman using this query
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.
答案2
得分: 0
我认为文档有误,应该使用POST请求而不是GET。
另外,文档中的JSON请求体存在错误:有一个不应该出现的尾逗号。请使用以下内容:
String body2 = "{\"pageSize\": \"100\"}";
英文:
I think the documentation is wrong and you're supposed to use a POST request instead of GET.
Also there is an error in the documented JSON request body: there is a trailing comma that should not be there. Use the following:
String body2 = "{\"pageSize\": \"100\"}";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论