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

发布于 2024-11-10 10:48:54 字数 4018 浏览 4 评论 0原文

第一次在这里提问。通常我不用问就能找到答案,但这一次我被困住了,无法弄清楚我错过了什么。

我只是想让我的 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);
    }
}

我已将表单和电子表格公开,因此请随意使用它并尝试让它自己工作。

我的程序没有出现任何错误,没有编译错误,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);
    }
}

我在应用程序中将 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");
}

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

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.

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.

Form

Spreadsheet

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.

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潜移默化 2024-11-17 10:48:54

所以我终于明白是怎么回事了。通过手动对表单 POST url 末尾的答案进行编码,我发现在查看源代码时给出的 url 存在其自身的编码问题。

这是来自源代码的 url:

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

但这是在上面的代码中实际工作所需的内容:

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

额外的 amp;是什么把事情搞砸了。不管出于什么原因,它也可以在没有最后一个 &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);
    }
}

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

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.

Here's the url from source:

<form action="https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&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

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

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

把人绕傻吧 2024-11-17 10:48:54

在许多情况下,entry.0.single 格式可能不起作用。您必须始终找到正确的元素 ID 才能创建 POST 请求。本文文章提供了通过以下方式将数据发布到 Google 文档表的正确方法安卓应用程序。

The format, entry.0.single might not work in many cases. You must always find the proper id of the elements to create your POST request. This article provides the proper way to post data to a Google docs sheet via android app.

饭团 2024-11-17 10:48:54

查看 Acra 的源代码。这会将堆栈跟踪上传到 Google 电子表格。

Have a look at the source for Acra. This uploads stack traces to a Google spreadsheet.

送你一个梦 2024-11-17 10:48:54

您可能没有看到错误,因为您以错误的方式打印异常。
您正在使用 e.printStackTrace(); ,它不会出现在 DDMS/Logcat 中。

你应该使用
Log.e("YOUR_TAG, "An error has gone", e);

它将在 DDMS/Logcat 中记录您的错误。您应该看到异常的堆栈跟踪,它将帮助您了解发生了什么编辑


您检查过 client.execute(post); 返回的内容吗?
您应该通过执行以下操作来检查 POST 响应中返回的内容:

HttpResponse httpResponse = client.execute(post);

您还可以在调试模式下运行应用程序并检查它崩溃/停止的位置

You probably are not seing errors because you are printing exceptions in the wrong way.
You are using e.printStackTrace(); which does not appear in DDMS/Logcat.

You should use instead
Log.e("YOUR_TAG, "An error has occurred", e);

which will log your error in DDMS/Logcat. You should see the stacktrace of the exception, and it will help you understand what's wrong.

EDIT:
Have you checked what is returned in client.execute(post); ?
You should check what is being returned in the POST response by doing:

HttpResponse httpResponse = client.execute(post);

You can also run the application in debug mode and check where it is crashing / stopping

你的往事 2024-11-17 10:48:54

因此,cardOneUrl 是layout.xml 中的文本编辑字段“results.add(new BasicNameValuePair("entry.0.single", cardOneURL));”谢谢,乔

So the cardOneUrl's are text edit fields in a layout.xml "results.add(new BasicNameValuePair("entry.0.single", cardOneURL));" Thanks, Joe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文