android - Google Spreadsheet API update\edit with protocol

标签 android apache xmlhttprequest google-api google-sheets

我正在尝试为 Google 电子表格实现一些基本功能, 将协议(protocol)规范与请求一起使用。

我这样做的原因是它适用于 Android,而 gdata-java 库并不能真正工作,而且 alpha android 也不能真正削减它。 我设法实现了身份验证、获取列表和删除,但是对于编辑\更新行我真的无法全神贯注。

例如我想改变一个单元格的内容 here是协议(protocol)的规范

根据我的理解,我必须向编辑 URL 发送某种原子或 xml 格式的请求。我从未发送过此类请求。

我还找到了this ,它给出了应该如何完成的线索(也是论坛上未回答的问题),但并没有真正管理它。

这是我的身份验证函数,如果您需要它,所以如果您想尝试编写代码,就不必再次实现它。

/**
 * Logs in to the Google service using the ClientLogin HttpRequest API and returns an authentification token
 */
 private String getAuthToken() {


HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(CLIENT_LOGIN_ADDRESS);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("Email", "username@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("Passwd", "password"));
nameValuePairs.add(new BasicNameValuePair("service", "wise"));
nameValuePairs.add(new BasicNameValuePair("source", SERVICE_NAME));

try {
  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
catch (UnsupportedEncodingException e) {
  //Log.e("ERROR", "UnsupportedEncodingException");
}

//Log.v("TEST", "Executing request " + httppost.getURI());

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;

try {
  responseBody = httpclient.execute(httppost, responseHandler);
}
catch (ClientProtocolException e) {
  //Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  //Log.e("ERROR", "IOException");
}

//Log.v("TEST", "response:" + responseBody);

String[] vals = responseBody.split("\n")[2].split("=");
String auth_string = vals[1];
//Log.v("TEST", "auth_token:" + vals[1]);

return auth_string;
}

这是我正在尝试的更新功能,请注意,如果您尝试,我的文档的编辑 URL 将不起作用,它只是为了我目前拥有的 iea:

/**
 * Ignore this i use it for testing
 */
 public void justTesting() {

String url = "http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1/1q0cdh";

HttpClient httpclient = new DefaultHttpClient();

HttpPut httpput = new HttpPut(url);
httpput.addHeader(new BasicHeader("Authorization", "GoogleLogin auth=" + getAuthToken()));
httpput.addHeader(new BasicHeader("GData-Version", "2.0"));

HttpEntity he = null;

String messageBody = "<entry>"+
                     "<id>https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1</id>"+
                     "<link rel=\"edit\" type=\"application/atom+xml\""+
                     "  href=\"https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1\"/>"+
                     "<gs:cell row=\"2\" col=\"2\" inputValue=\"300\"/>"+
                      "</entry>";


try {
  he = new StringEntity(messageBody);
}
catch (UnsupportedEncodingException e) {
  //Log.e("ERROR", "UnsupportedEncodingException");
}

httpput.setEntity(he);


try {

  HttpResponse hr = httpclient.execute(httpput);
  //Log.d("DEBUG", "sl : " + hr.getStatusLine());
}
catch (ClientProtocolException e) {
  //Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  //Log.e("ERROR", "IOException");
}

//Log.v("TEST", "executed");

}

这目前给出了一个 400 Bad 请求。 我也在尝试使用 Apache HttpClient 来实现这一点。

有没有人知道如何实现\实现\我应该如何发送什么请求?

谢谢,非常感谢您的帮助。


我取得了一些进展并写下了这个:

public void justTesting() {

String url = "https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1";

HttpClient httpclient = new DefaultHttpClient();

HttpPut httpput = new HttpPut(url);
httpput.addHeader(new BasicHeader("Authorization", "GoogleLogin auth=" + getAuthToken()));
httpput.addHeader(new BasicHeader("GData-Version", "3.0"));

HttpEntity he = null;

String messageBody = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'> <id>http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1 </id> <gs:cell row='2' col='1' inputValue='mouuuuuuuuusee'/> </entry>";
  //RequestEntity requestEntity = new StringRequestEntity(xml.toString(), "application/atom+xml", "UTF-8");


try {
  he = new StringEntity(messageBody);
}
catch (UnsupportedEncodingException e) {
  Log.e("ERROR", "UnsupportedEncodingException");
}

httpput.addHeader("Content-Type", "application/atom+xml");
httpput.setEntity(he);


try {
  HttpResponse hr = httpclient.execute(httpput);
  Log.d("DEBUG", "sl : " + hr.getStatusLine());
}
catch (ClientProtocolException e) {
  Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  Log.e("ERROR", "IOException");
}

Log.v("TEST", "executed");

}

现在它给了我一个 403 Forbidden。知道为什么吗?

最佳答案

嘿,我想通了,我错过了这个:

httpput.addHeader(new BasicHeader("If-Match", "*"));

现在函数看起来像这样:

  public void justTesting() {

String url = "http://spreadsheets.google.com/feeds/cells/t9VU1IwRrmG3h-nhI_J2fzg/od6/private/full/R2C1";

HttpClient httpclient = new DefaultHttpClient();

HttpPut httpput = new HttpPut(url);
httpput.addHeader(new BasicHeader("Authorization", "GoogleLogin auth=" + getAuthToken()));
httpput.addHeader(new BasicHeader("GData-Version", "2.0"));
httpput.addHeader(new BasicHeader("If-Match", "*"));

HttpEntity he = null;

String messageBody = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'> <id>http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1</id> <link rel='edit' type='application/atom+xml' href='http://spreadsheets.google.com/feeds/cells/t9VU1IwRrmG3h-nhI_J2fzg/od6/private/full/R2C1/1en5'/> <gs:cell row='2' col='1' inputValue='mouuuuuuuuusee' /> </entry>";

try {
  he = new StringEntity(messageBody);
}
catch (UnsupportedEncodingException e) {
  Log.e("ERROR", "UnsupportedEncodingException");
}

httpput.addHeader("Content-Type", "application/atom+xml");
httpput.setEntity(he);


try {
  HttpResponse hr = httpclient.execute(httpput);
  Log.d("DEBUG", "sl : " + hr.getStatusLine());
}
catch (ClientProtocolException e) {
  Log.e("ERROR", "ClientProtocolException");
}
catch (IOException e) {
  Log.e("ERROR", "IOException");
}

Log.v("TEST", "executed");

关于android - Google Spreadsheet API update\edit with protocol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3254330/

相关文章:

php - 通过使用 Spatie URL-Signer 生成有限生命周期的链接来保护文件

javascript - 异步 xmlhttprequests 失败,同步则不然

java - android: TextToSpeech 泄漏服务连接

java - 正确地将一堆项目添加到 ArrayList

java - 如何使用 Android 注释通过 Intent 获取额外数据?

php - 如何在浏览器中在线查看PHP或Apache错误日志?

java - mac tomcat installation : Cannot find/usr/local/apache-tomcat-8. 0.9/bin/setclasspath.sh 运行这个程序需要这个文件

angularjs - Angular 1.5.7 - $http POST 请求被浏览器取消

javascript - XMLHttpRequest 错误进度

java - ServerSocket.accept() 抛出 SocketTimeoutException 并显示 null 消息