15

第一次在这里提问。通常我可以在不问的情况下找到我的答案,但这次我被卡住了,无法弄清楚我错过了什么。

我只是想让我的 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 代码全部作为一行返回。如我错了请纠正我。

4

5 回答 5

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">

但这是在上述代码中实际工作所需要的:

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

额外的放大器;是什么搞砸了。无论出于何种原因,它也可以在没有最后一个 &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 为我指明了正确的方向。

于 2011-06-02T00:20:15.030 回答
5

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

于 2015-02-23T10:08:30.773 回答
2

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

于 2011-06-01T23:55:20.697 回答
1

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

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

这将在 DDMS/Logcat 中记录您的错误。您应该看到异常的堆栈跟踪,它将帮助您了解问题所在。

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

HttpResponse httpResponse = client.execute(post);

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

于 2011-05-30T11:14:20.550 回答
0

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

于 2012-10-21T00:34:50.353 回答