こちらで初めて質問します。通常、質問しなくても答えを見つけることができますが、今回は行き詰まって何が欠けているのかわかりません。
Android アプリで Web サイトのフォームに記入して送信しようとしています。送り返されるデータをアプリで処理する必要はありません。フォームに入力して送信するだけです。基本的に、投票アプリの結果を収集しようとしています。フォームの送信は簡単だと思ったので、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 では行の 1 つの出力のみがログに記録されることにも注意してください。これは、html コードがすべて 1 行で返されるためだと思います。私が間違っている場合は修正してください。