如何解决使用Android提交到Google Spreadsheet表单
| 第一次在这里问一个问题。通常,我无需问就能找到答案,但是这次我陷入困境,无法弄清自己所缺少的是什么。 我只是想让我的Android应用程序在网站上填写表格并提交。我不需要应用程序对发送回的任何数据执行任何操作,只需填写表格并提交即可。基本上,我正在尝试收集投票应用程序的结果。我以为提交表单很简单,所以我创建了一个Google Spreadsheet并用它制成了表单。我以为可以将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\",e);
} catch (IOException e) {
// Auto-generated catch block
Log.e(\"YOUR_TAG\",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\",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\",\"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 Spreadsheet Forms的人有所帮助。感谢@pandre为我指出正确的方向。
, 在很多情况下,entry.0.single格式可能不起作用。您必须始终找到要创建POST请求的元素的正确ID。本文提供了通过android应用将数据发布到Google文档表的正确方法。
, 看看Acra的来源。这会将堆栈跟踪上传到Google电子表格。
, 您可能没有发现错误,因为您以错误的方式打印了异常。
您使用的是e.printStackTrace();
,它没有出现在DDMS / Logcat中。
您应该改用
Log.e(\"YOUR_TAG,e);
这会将您的错误记录在DDMS / Logcat中。您应该看到异常的堆栈跟踪,它将帮助您了解错误所在。
编辑:
您是否已检查returned8ѭ返回了什么?
您应该通过执行以下操作检查POST响应中返回的内容:
HttpResponse httpResponse = client.execute(post);
您还可以在调试模式下运行该应用程序,并检查它在哪里崩溃/停止
, 因此,cardOneUrl \是layout.xml中的文本编辑字段\“ results.add(new BasicNameValuePair(\” entry.0.single \“,cardOneURL)); \”谢谢,乔
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。