使用 Android 提交到 Google 电子表格表单 [英] Using Android to submit to a Google Spreadsheet Form

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

问题描述

第一次在这里提问.通常我不用问就可以找到答案,但这次我卡住了,无法弄清楚我错过了什么.

First time asking a question here. Usually I can find my answer without having to ask, but this time I'm stuck and can't figure out what I'm missing.

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

I'm just trying to have my Android app fill out a form on a website and submit it. I don't need the app to do anything with any data being sent back, just fill out the form and submit it. Basically I'm trying to collect the results of a voting app. I thought form submission would be simple so I created a Google Spreadsheet and made a form out of it. I figured I'd point the Android app to the form and then I would have all the data in the spreadsheet for later viewing. I can't get the Android app to actually fill out the form though. Here's the resources.

表单

电子表格

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);
    }
}

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

I made both the form and the spreadsheet public so feel free to mess around with it and try and get it to work yourself.

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

I get no errors from my program, no compile errors, no errors in DDMS. When I actually run the program and click the button that executes this code I can see the delay since right now this is in the UI thread, so I know it's executing it. It appears as if everything is working perfectly, but my spreadsheet doesn't update at all.

有什么想法吗?我确定我错过了一些愚蠢的事情,但任何帮助将不胜感激.

Any thoughts? I'm sure it's something stupid that I'm missing, but any help would be appreciated.

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

Here's the updated code with lots of logging and debugging stuff.

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);
    }
}

我将 ReadingView.class 用于我的应用程序中的其他内容,但现在为了这个记录目的而劫持了它.它只有一个 onCreate() 方法,如下所示.

I use ReadingView.class for something else in my app, but hijacked it for this logging purpose right now. It only has an onCreate() method, which is below.

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");
}

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

Also worth noting that in the DDMS it only logs one output of line. I believe this is just because the html code is returned all as one line. Correct me if I'm wrong.

推荐答案

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

So I finally figured out what was going on. Through messing with manually encoding answers to the end of the form POST url I was able to find that the url it gave when viewing the source had encoding issues of it's own in it.

这是来源的网址:

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

但这里是实际使用上述代码所需的内容:

But here's what it needs to be to actually work in the above code:

https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ

额外的放大器;是什么搞砸了.无论出于何种原因,它也可以在没有最后一个 &ifq 的情况下工作,所以我忽略了它.无论如何,这是完整的代码:

The extra amp; was what was messing it up. For whatever reason it works without the last &ifq too, so I left that off. Anyway, here's completed code:

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);
    }
}

希望这对尝试使用 Google 电子表格表单的其他人有所帮助.并感谢@pandre 为我指明了正确的方向.

Hope this helps someone else when trying to work with Google Spreadsheet Forms. And thanks to @pandre for pointing me in the right direction.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆