Using HttpURLConnection on Android to submit to a Google Form

随声附和 提交于 2019-12-11 19:41:46

问题


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
    }


来源:https://stackoverflow.com/questions/33313596/using-httpurlconnection-on-android-to-submit-to-a-google-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!