1

I would like to submit a string into a Google Form from an Android app. It seems that this was achieved in this thread but since then the Apache HTTP client was removed with Android 6.0.

It was suggested to use OkHttp in this thread but OkHttp has since been updated and the FormEncodingBuilder class is gone. What is the up-to-date way to submit to a Google Form?

Community
  • 1
  • 1
ModWilliam
  • 45
  • 7

1 Answers1

3

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/

  • How do I use FormBody? Doesn't seem to be used in the same way that the OkHttp thread used it. – ModWilliam Jan 27 '16 at 21:34
  • That is basically what I have. Right now, there are blank submissions into my google spreadsheet. So, there's just a timestamp and no data. I logged the toString of the Request after it was built and got: "Request{method=POST, url=https://docs.google.com/forms/d/1ZGf2blvjF[RANDOM_LETTERS]YY0wE/formResponse, tag=null}" Could the URL I'm using be the wrong one? The URLs in the other posts are different but they are much older, of course. Just wouldn't know which URL to use – ModWilliam Jan 28 '16 at 01:01
  • Thanks! This is exactly what I needed. – ModWilliam Jan 28 '16 at 19:07