第一次在这里提问。通常我不用问就能找到我的答案,但这次我卡住了,不知道我错过了什么。
我只是想让我的 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&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);
}
}
我在我的应用程序中将 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 代码全部作为一行返回。如果我错了,请纠正我。
最佳答案
所以我终于弄明白是怎么回事了。通过对 POST 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
额外的放大器;是什么搞砸了。无论出于何种原因,它也可以在没有最后一个 &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 为我指明了正确的方向。
关于android - 使用 Android 提交到 Google 电子表格表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174962/