【发布时间】:2020-07-08 17:12:16
【问题描述】:
在我的 android 应用程序中,我使用 okhttp:3.10.0 创建一个 httpclient 来向 google drive (REST V3) api 发出请求。我想在用户谷歌驱动器中创建一个文件夹。我读了google documentation 来创建一个文件夹。这个文档建议有这样的东西
File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
我也经历过this question。我正在制作这样的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;
我正在尝试使用 json 对象,但它没有转换为请求正文,因为它无法解析同伴
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());
现在我不确定如何在请求正文中添加该元数据。有人知道怎么做吗?
【问题讨论】:
标签: java android google-drive-api okhttp