I used the method in 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. How can I post to a Google Spreadsheet form using HttpURLConnection?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I assume that the method you post a Google Spreadsheet is similar to the answer of this question.
I recommend you to use a 3rd party library which called okhttp.
okhttp is reliable, easy to use, and it also has several additional features.
The following is the sample code. Hope it would be helpful.
// 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
}