在 Android 上使用 HttpURLConnection 提交到 Google 表单
Using HttpURLConnection on Android to submit to a Google Form
我使用了this question to post to a Google Spreadsheet form from my Android app. The method in the question uses the Apache HTTP Client which was removed in Android 6.0. While it is possible to use the Apache HTTP Client on Android 6.0, I'd like to implement the same functionally with HttpURLConnection中的方法。如何使用 HttpURLConnection post 到 Google 电子表格?
我假设您 post 一个 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
}
我使用了this question to post to a Google Spreadsheet form from my Android app. The method in the question uses the Apache HTTP Client which was removed in Android 6.0. While it is possible to use the Apache HTTP Client on Android 6.0, I'd like to implement the same functionally with HttpURLConnection中的方法。如何使用 HttpURLConnection post 到 Google 电子表格?
我假设您 post 一个 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
}