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&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;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.

Leave a Comment