当前位置: 代码迷 >> Android >> 在Android上使用HttpURLConnection提交到Google表单
  详细解决方案

在Android上使用HttpURLConnection提交到Google表单

热度:51   发布时间:2023-08-04 11:35:16.0

我使用问题中的方法从我的Android应用发布到Google Spreadsheet表单。 问题中的方法使用在Android 6.0中的Apache HTTP客户端。 虽然可以在Android 6.0上使用Apache HTTP客户端,但我想在上实现相同的功能。 如何使用HttpURLConnection发布到Google Spreadsheet表单?

我认为您发布Google Spreadsheet的方法类似于的答案。

我建议您使用一个名为第三方库。

okhttp可靠,易于使用,并且还具有其他一些功能。

以下是示例代码。 希望对您有所帮助。

    // 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
    }
  相关解决方案