将数据发布到新的Google表单

时间:2013-04-05 01:03:22

标签: java google-sheets http-post google-form

使用之前(旧版)的google表单,可以通过将HttpPost发送到这样的网址以编程方式将数据发布到表单:

https://spreadsheets.google.com/formResponse?formkey=[key]

和这样的数据:

entry.1.single=data&entry.2.single=moredata

随着谷歌表格的新版本(我认为2013年1月发布),网址结构不同。以下是“查看实时表单页面”的片段

<form action="https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse" method="POST" ...
<input type="text" name="entry.1566150510"

从这段代码中,我认为我可以发布到这样的网址:

https://docs.google.com/forms/d/[key]/formResponse

使用这样的数据:

entry.1566150510=data

但是我已经尝试过这样的java(android):

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    Log.i(myTag, "Inside postData()");
    String fullUrl = "https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse";
    Log.i(myTag, "url = " + fullUrl);
    HttpPost httppost = new HttpPost(urlBase);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("entry.1566150510", "somedata"));
        UrlEncodedFormEntity data = new UrlEncodedFormEntity(nameValuePairs);
        Log.i(myTag, data.toString());
        httppost.setEntity(data);
        Log.i(myTag, EntityUtils.toString(data));
        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
        Log.i("DocsUploader", "response = " + response);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
} 

我得到404,没有找到回应。

我是否遗漏了一些明显的东西,或谷歌刚刚删除了发布到新表单的能力?

2 个答案:

答案 0 :(得分:2)

考虑使用google-apps-script进行更新(我怀疑它的工作量会减少)。即使谷歌做出更多调整,它仍将继续有效。

答案 1 :(得分:2)

好的,关于我使用HttpClient和HttpPost的事情是错误的。我不确切地知道我搞砸了什么,但我现在正在使用它并且它正常工作:

public void postData() {
    String fullUrl = "https://docs.google.com/forms/d/1Ee330GpkMHX_0dKWmJb6ZPdm4FBhhqJSqBbgysEtq6M/formResponse";
    HttpRequest mReq = new HttpRequest();
    String response = mReq.sendPost(fullUrl, "entry.1566150510=data");
    Log.i(myTag, response);
} 

其中HttpRequest是由MattC创建的助手类,并作为这个问题的答案发布:Secure HTTP Post in Android