【问题标题】:Using HttpURLConnection on Android to submit to a Google Form在 Android 上使用 HttpURLConnection 提交到 Google 表单
【发布时间】:2015-10-24 01:27:37
【问题描述】:
我使用this 问题中的方法从我的 Android 应用程序发布到 Google 电子表格表单。问题中的方法使用 Apache HTTP 客户端,它是 Android 6.0 中的 removed。虽然可以在 Android 6.0 上使用 Apache HTTP 客户端,但我想用HttpURLConnection 实现相同的功能。如何使用 HttpURLConnection 发布到 Google 电子表格表单?
【问题讨论】:
标签:
android
post
google-sheets
httpurlconnection
google-forms
【解决方案1】:
我假设您发布 Google 电子表格的方法类似于 this question 的答案。
我建议您使用名为 okhttp 的第 3 方库。
okhttp 可靠、易于使用,并且还具有几个附加功能。
以下是示例代码。希望对您有所帮助。
// perpare httpclient
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(20, TimeUnit.SECONDS);
client.setReadTimeout(20, TimeUnit.SECONDS);
// prepare post params
RequestBody body = new FormEncodingBuilder()
.add("entry.0.single", cardOneURL)
.add("entry.1.single", outcome)
.add("entry.2.single", cardTwoURL)
.build();
// prepare request
Request request = new Request.Builder()
.url("https://spreadsheets.google.com/spreadsheet/your_spreadsheet")
.post(body)
.build();
try {
client.newCall(request).execute(); // send your request
} catch (Exception ex) {
// do something
}