采用Android递交给谷歌小号preadsheet表 [英] Using Android to submit to a Google Spreadsheet Form

查看:172
本文介绍了采用Android递交给谷歌小号preadsheet表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次问一个问题在这里。通常我能找到我的答案,而不必问,但这次我坚持并不能找出我错过了什么。

我只是想有我的Andr​​oid应用程序填写表格在网站上,并提交。我不需要应用程序做任何事情与正在发送的任何数据,则只需填写表格并提交。基本上我试图收集的表决程序的结果。我以为表单提交会简单,所以我创建了一个谷歌小号preadsheet,并提出了形式出来。我想我点了Android应用程序的窗体,然后我会在s preadsheet全部数据供以后观看。我不能让Android应用程序实际填写表格,虽然。这里的资源。

<一个href="https://s$p$padsheets.google.com/s$p$padsheet/viewform?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&ifq">Form

<一个href="https://s$p$padsheets.google.com/s$p$padsheet/ccc?key=0ApTl61fIWzKqdDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc&hl=en_US">S$p$padsheet

 私人无效submitVote(字符串的结果){
    HttpClient的客户端=新DefaultHttpClient();
    HttpPost后=新HttpPost("https://s$p$padsheets.google.com/s$p$padsheet/formResponse?hl=en_US&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;ifq");

    名单&LT; BasicNameValuePair&GT;结果=新的ArrayList&LT; BasicNameValuePair&GT;();
    results.add(新BasicNameValuePair(entry.0.single,cardOneURL));
    results.add(新BasicNameValuePair(entry.1.single,结局));
    results.add(新BasicNameValuePair(entry.2.single,cardTwoURL));

    尝试 {
        post.setEntity(新UrlEn codedFormEntity(结果));
    }赶上(UnsupportedEncodingException E){
        //自动生成的catch块
        Log.e(YOUR_TAG,出现错误,E);
    }
    尝试 {
        client.execute(后);
    }赶上(ClientProtocolException E){
        //自动生成的catch块
        Log.e(YOUR_TAG,出现错误,E);
    }赶上(IOException异常E){
        //自动生成的catch块
        Log.e(YOUR_TAG,出现错误,E);
    }
}
 

我做的形式和在S preadsheet公众可以随意摆弄它和尝试,并得到它的工作吧。

我没有得到任何错误,从我的程序,没有编译错误,在DDMS没有错误。当我实际运行该程序,并单击执行这个code中的按钮,我可以看到的延迟,因为现在这是在UI线程,所以我知道它的执行它。它看起来好像一切都工作的很好,但是我的小号preadsheet不更新的。

有什么想法?我敢肯定,这是愚蠢的东西我失踪,但任何帮助将是AP preciated。

下面是更新后的code与大量的日志记录和调试的东西。

 私人无效submitVote(字符串的结果){
    HttpClient的客户端=新DefaultHttpClient();
    HttpPost后=新HttpPost("https://s$p$padsheets.google.com/s$p$padsheet/formResponse?hl=en_US&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;ifq");

    名单&LT; BasicNameValuePair&GT;结果=新的ArrayList&LT; BasicNameValuePair&GT;();
    results.add(新BasicNameValuePair(entry.0.single,cardOneURL));
    results.add(新BasicNameValuePair(entry.1.single,结局));
    results.add(新BasicNameValuePair(entry.2.single,cardTwoURL));

    尝试 {
        post.setEntity(新UrlEn codedFormEntity(结果));
    }赶上(UnsupportedEncodingException E){
        //自动生成的catch块
        Log.e(YOUR_TAG,出现错误,E);
    }
    尝试 {
        HTT presponse HTT presponse = client.execute(后);
        Log.e(响应,信息:+ HTT presponse);

        的BufferedReader RD =新的BufferedReader(新的InputStreamReader(HTT presponse.getEntity()的getContent()));
        串线;
        而((行= rd.readLine())!= NULL){
        Log.i(言,行);
        }

        意向意图=新的意图(这一点,ReadingView.class);
        intent.putExtra(HTML,行);
        startActivity(意向);
    }赶上(ClientProtocolException E){
        //自动生成的catch块
        Log.e(YOUR_TAG,客户端协议异常,E);
    }赶上(IOException异常E){
        //自动生成的catch块
        Log.e(YOUR_TAG,IO异常,E);
    }
}
 

我用ReadingView.class在我的应用程序别的东西,但它劫持这个记录的目的现在。它只有一个的onCreate()方法,它是低于

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.readingview);

    的WebView mWebView =(web视图)findViewById(R.id.webview);
    mWebView.getSettings()setJavaScriptEnabled(真)。
    //mWebView.loadUrl(getIntent().getExtras().getString("url));
    mWebView.loadData(getIntent()getExtras()的getString(HTML),text / html的,UTF-8);
}
 

另外值得一提的是,在DDMS它只记录行的一个输出。我相信,这仅仅是因为HTML code返回所有为一行。纠正我,如果我错了。

解决方案

所以,我终于想通了,发生了什么事。通过与手工编码的答案搞乱到表单POST网址的结尾我能发现它查看源代码时提供的网址有它自己的编码问题在里面。

下面是从源代码的网址:

 &LT;形式action="https://s$p$padsheets.google.com/s$p$padsheet/formResponse?hl=en_US&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;ifq"方法=POSTID =SS型&GT;
 

但这里是它需要实际工作中的上述code:

<$p$p><$c$c>https://s$p$padsheets.google.com/s$p$padsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ

额外的放大器;是什么被搞乱它。无论出于何种原因,它不工作的最后一个与放大器; IFQ了,所以我离开了这关。总之,这里的完成code:

 私人无效submitVote(字符串的结果){
    HttpClient的客户端=新DefaultHttpClient();
    HttpPost后=新HttpPost("https://s$p$padsheets.google.com/s$p$padsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ");

    名单&LT; BasicNameValuePair&GT;结果=新的ArrayList&LT; BasicNameValuePair&GT;();
    results.add(新BasicNameValuePair(entry.0.single,cardOneURL));
    results.add(新BasicNameValuePair(entry.1.single,结局));
    results.add(新BasicNameValuePair(entry.2.single,cardTwoURL));

    尝试 {
        post.setEntity(新UrlEn codedFormEntity(结果));
    }赶上(UnsupportedEncodingException E){
        //自动生成的catch块
        Log.e(YOUR_TAG,出现错误,E);
    }
    尝试 {
        client.execute(后);
    }赶上(ClientProtocolException E){
        //自动生成的catch块
        Log.e(YOUR_TAG,客户端协议异常,E);
    }赶上(IOException异常E){
        //自动生成的catch块
        Log.e(YOUR_TAG,IO异常,E);
    }
}
 

希望这可以帮助其他人试图与谷歌小号preadsheet形式来工作的时候。并且由于@pandre指着我在正确的方向。

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

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.

解决方案

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&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;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.

这篇关于采用Android递交给谷歌小号preadsheet表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆