使用Android提交到Google电子表格表单

doe*_*qts 15 post android spreadsheet google-forms

第一次在这里问一个问题.通常我可以在不必询问的情况下找到答案,但这次我被困住了,无法弄清楚我错过了什么.

我只是想让我的Android应用在网站上填写表格并提交.我不需要应用程序对发回的任何数据执行任何操作,只需填写表单并提交即可.基本上我正在尝试收集投票应用的结果.我认为表单提交很简单所以我创建了一个Google电子表格并从中制作了一个表单.我想我会将Android应用程序指向表单,然后我会在电子表格中包含所有数据供以后查看.我不能让Android应用程序实际填写表格.这是资源.

形成

电子表格

private void submitVote(String outcome) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&ifq");

    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", "An error has occurred", e);
    } catch (IOException e) {
        // Auto-generated catch block
        Log.e("YOUR_TAG", "An error has occurred", e);
    }
}
Run Code Online (Sandbox Code Playgroud)

我公开了表单和电子表格,所以请随意使用它并尝试让它自己工作.

我的程序没有错误,没有编译错误,DDMS没有错误.当我实际运行程序并单击执行此代码的按钮时,我可以看到延迟,因为现在这是在UI线程中,所以我知道它正在执行它.看起来好像一切正​​常,但我的电子表格根本没有更新.

有什么想法吗?我确信我错过了一些愚蠢的事情,但任何帮助都会受到赞赏.

这是更新的代码,包含大量的日志记录和调试内容.

private void submitVote(String outcome) {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;ifq");

    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 {
        HttpResponse httpResponse = client.execute(post);
        Log.e("RESPONSE", "info: " + httpResponse);

        BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        String line;
        while ((line = rd.readLine()) != null) {
        Log.i("words", line);   
        }

        Intent intent = new Intent(this, ReadingView.class);
        intent.putExtra("html", line);
        startActivity(intent);
    } 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中使用ReadingView.class作为其他东西,但是现在劫持它用于此日志记录目的.它只有一个onCreate()方法,如下所示.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.readingview);

    WebView mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    //mWebView.loadUrl(getIntent().getExtras().getString("url"));
    mWebView.loadData(getIntent().getExtras().getString("html"), "text/html", "utf-8");
}
Run Code Online (Sandbox Code Playgroud)

另外值得注意的是,在DDMS中它只记录一行输出.我相信这只是因为html代码全部作为一行返回.如我错了请纠正我.

doe*_*qts 18

所以我终于弄清楚发生了什么.通过手动编码表单POST url末尾的答案,我能够发现它在查看源代码时给出的url具有自己的编码问题.

这是来自源的网址:

<form action="https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;ifq" method="POST" id="ss-form">
Run Code Online (Sandbox Code Playgroud)

但这是实际使用上述代码所需要的:

https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ
Run Code Online (Sandbox Code Playgroud)

额外的放大器; 是什么弄乱了它.无论出于何种原因,它都可以在没有最后一个&ifq的情况下工作,所以我把它关了.无论如何,这里完成的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助其他人尝试使用Google Spreadsheet Forms.感谢@pandre指出我正确的方向.


Sou*_*h86 5

在许多情况下,格式entry.0.single可能不起作用.您必须始终找到要创建POST请求的元素的正确ID.该文章提供的数据后通过Android应用程序谷歌的文档纸张的正确方法.