在Android上使用HttpURLConnection提交给G

在Android上使用HttpURLConnection提交给G

本文介绍了在Android上使用HttpURLConnection提交给Google表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了问题中的方法从我的Android应用发布到Google Spreadsheet表单。问题中的方法使用Apache HTTP Client,它是在Android 6.0中。虽然可以在Android 6.0上使用Apache HTTP客户端,但我希望在功能上与。如何使用HttpURLConnection发布到Google Spreadsheet表单?

解决方案

我假设您发布Google Spreadsheet的方法类似于的答案。



我建议您使用名为 okhttp 的第三方库, a>。



okhttp 是可靠,易于使用的,并且还具有其他几项功能。

以下是示例代码。希望这会有帮助。

  // perpare httpclient 
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(20,TimeUnit.SECONDS);
client.setReadTimeout(20,TimeUnit.SECONDS);

//准备后参数
RequestBody body = new FormEncodingBuilder()
.add(entry.0.single,cardOneURL)
.add(entry .single,结果)
.add(entry.2.single,cardTwoURL)
.build();

//准备请求
请求请求= new Request.Builder()
.url(https://spreadsheets.google.com/spreadsheet/your_spreadsheet)
.post(body)
.build();

尝试{

client.newCall(request).execute(); //发送您的请求

} catch(Exception ex){

//做某事
}


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?

解决方案

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
    }

这篇关于在Android上使用HttpURLConnection提交给Google表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:59