[英]Creating a okhttp3 request body to create a folder in google drive
In my android app I am using okhttp:3.10.0 to create a httpclient to make a request to google drive (REST V3) api.在我的 android 应用程序中,我使用 okhttp:3.10.0 创建一个 httpclient 以向谷歌驱动器(REST V3)api 发出请求。 I want to create a folder in users google drive.
我想在用户谷歌驱动器中创建一个文件夹。 I read google documentation to create a folder.
我阅读了谷歌文档来创建一个文件夹。 This documentation suggest to have something like this
该文档建议有这样的东西
File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
I went through this question as well.我也经历了这个问题。 I am making the okhttpclient like this
我正在像这样制作 okhttpclient
OkHttpClient client = new OkHttpClient();
RequestBody body = ????????
Request request = new Request.Builder()
.url("https://www.googleapis.com/drive/v3/files")
.addHeader("Authorization", String.format("Bearer %s", tokens[0]))
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
String jsonBody = response.body().string();
Log.i(LOG_TAG, String.format("User Info Response %s", jsonBody));
return new JSONObject(jsonBody);
} catch (Exception exception) {
Log.w(LOG_TAG, exception);
}
return null;
I am trying to use a json object but it is not converting to request body because it can not resolve companion我正在尝试使用 json object 但它没有转换为请求正文,因为它无法解析同伴
import okhttp3.MediaType.Companion.toMediaTypeOrNull;
import okhttp3.RequestBody.Companion.toRequestBody;
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("title", "Ancd test");
jsonObject.put("mimeType", "application/vnd.google-apps.folder");
} catch (JSONException e) {
e.printStackTrace();
}
String body = jsonObject.toString().toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull());
Now I am not sure how to add that meta data in request body.现在我不确定如何在请求正文中添加该元数据。 Anyone knows how to do this?
任何人都知道如何做到这一点?
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(jsonObject.toString(), mediaType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.