Using Android to Submit to a Google Spreadsheet Form

Using Android to submit to a Google Spreadsheet Form

So I finally figured out what was going on. Through messing with manually encoding answers to the end of the form POST url I was able to find that the url it gave when viewing the source had encoding issues of it's own in it.

Here's the url from source:

<form action="https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&ifq" method="POST" id="ss-form">

But here's what it needs to be to actually work in the above code:

https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ

The extra amp; was what was messing it up. For whatever reason it works without the last &ifq too, so I left that off. Anyway, here's completed code:

private void submitVote(String outcome) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ");

List<BasicNameValuePair> results = new ArrayList<BasicNameValuePair>();
results.add(new BasicNameValuePair("entry.0.single", cardOneURL));
results.add(new BasicNameValuePair("entry.1.single", outcome));
results.add(new BasicNameValuePair("entry.2.single", cardTwoURL));

try {
post.setEntity(new UrlEncodedFormEntity(results));
} catch (UnsupportedEncodingException e) {
// Auto-generated catch block
Log.e("YOUR_TAG", "An error has occurred", e);
}
try {
client.execute(post);
} catch (ClientProtocolException e) {
// Auto-generated catch block
Log.e("YOUR_TAG", "client protocol exception", e);
} catch (IOException e) {
// Auto-generated catch block
Log.e("YOUR_TAG", "io exception", e);
}
}

Hope this helps someone else when trying to work with Google Spreadsheet Forms. And thanks to @pandre for pointing me in the right direction.

Submit to Google Form from Android?

You now can use : okhttp3.FormBody instead of FormEncodingBuilder.

Edit :
Something like this

OkHttpClient client = new OkHttpClient();
FormBody body = new FormBody.Builder()
.add("your_param_1", "your_value_1")
.add("your_param_2", "your_value_2")
.build();
Request request = new Request.Builder()
.url("http://my.wonderfull.url/to/post")
.post(body)
.build();
Response response = client.newCall(request).execute();

If you want to post JSON content replace the FromBody by a RequestBody:

RequestBody body = RequestBody.create( JSON, json );

Edit 2:

The URL to use is (this one is mine):

https://docs.google.com/forms/d/1k96sccTC_24b6jo9Fs4h_ML-FVKHCMElgjUzOwSkwu4/formResponse

And you also have to find the name of your form inputs by looking at the source code of the form, the name starts with entry. following by a number:

<input name="entry.361168075" value="" class="ss ......

So your formBody looks like this:

FormBody body = new FormBody.Builder()
.add( "entry.361168075", "Red" )
.build();

I made up a full workable example there :
http://blog.quidquid.fr/2016/01/post-from-java-to-a-google-docs-form/

Using HttpURLConnection on Android to submit to a Google Form

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
}


Related Topics



Leave a reply



Submit