Android 谷歌电子表格API更新\使用协议编辑

Android 谷歌电子表格API更新\使用协议编辑,android,apache,xmlhttprequest,google-api,google-sheets,Android,Apache,Xmlhttprequest,Google Api,Google Sheets,我正在尝试为Google电子表格实现一些基本功能, 在请求中使用协议规范 我之所以这么做,是因为它是针对Android的,GDataJava库不能真正工作,而alpha Android库也不能真正解决这个问题。 我成功地实现了身份验证、获取列表和删除,但对于编辑\更新行,我真的无法完全理解它 例如,我想更改单元格的内容 是协议的规范 根据我的理解,我必须向编辑URL发送某种atom或xml格式的请求。我从未发出过这样的请求 我还发现,这给了一个应该如何做的线索(在论坛上也是一个未回答的问题),但

我正在尝试为Google电子表格实现一些基本功能, 在请求中使用协议规范

我之所以这么做,是因为它是针对Android的,GDataJava库不能真正工作,而alpha Android库也不能真正解决这个问题。 我成功地实现了身份验证、获取列表和删除,但对于编辑\更新行,我真的无法完全理解它

例如,我想更改单元格的内容 是协议的规范

根据我的理解,我必须向编辑URL发送某种atom或xml格式的请求。我从未发出过这样的请求

我还发现,这给了一个应该如何做的线索(在论坛上也是一个未回答的问题),但并没有真正做到这一点

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

/**
 * 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;
}
/**
*使用ClientLogin HttpRequest API登录到Google服务,并返回身份验证令牌
*/
私有字符串getAuthToken(){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(客户端登录地址);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“accountType”、“GOOGLE”);
添加(新的BasicNameValuePair(“电子邮件”)username@gmail.com"));
添加(新的BasicNameValuePair(“Passwd”、“password”);
添加(新的BasicNameValuePair(“服务”、“智能”));
添加(新的BasicNameValuePair(“源”,服务名称));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}
捕获(不支持的编码异常e){
//Log.e(“错误”、“不支持的编码异常”);
}
//Log.v(“测试”,“执行请求”+httppost.getURI());
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=null;
试一试{
responseBody=httpclient.execute(httppost,responseHandler);
}
捕获(客户端协议例外e){
//Log.e(“错误”、“客户端协议异常”);
}
捕获(IOE异常){
//Log.e(“错误”、“IOException”);
}
//Log.v(“测试”,“响应:+responseBody”);
字符串[]vals=responseBody.split(“\n”)[2]。split(“=”;
字符串auth_String=vals[1];
//Log.v(“测试”,“验证令牌:+VAL[1]);
返回auth_字符串;
}
以下是我正在尝试的更新功能,请注意,编辑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");

}
/**
*忽略这一点,我使用它进行测试
*/
公共测试{
字符串url=”http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1/1q0cdh";
HttpClient HttpClient=新的DefaultHttpClient();
HttpPut HttpPut=新的HttpPut(url);
httpput.addHeader(新的BasicHeader(“授权”、“谷歌登录验证=“+getAuthToken()));
addHeader(新的BasicHeader(“GData版本”,“2.0”);
httphe=null;
字符串messageBody=“”+
"https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1"+
""+
""+
"";
试一试{
he=新的StringEntity(messageBody);
}
捕获(不支持的编码异常e){
//Log.e(“错误”、“不支持的编码异常”);
}
httpput.setEntity(he);
试一试{
HttpResponse hr=httpclient.execute(httpput);
//Log.d(“调试”,“sl:+hr.getStatusLine());
}
捕获(客户端协议例外e){
//Log.e(“错误”、“客户端协议异常”);
}
捕获(IOE异常){
//Log.e(“错误”、“IOException”);
}
//Log.v(“测试”、“执行”);
}
这目前给出了400个错误请求。 我还试图使用ApacheHttpClient实现这一点

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

谢谢,非常感谢你的帮助


我取得了一些进展,并写了以下内容:

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");

}
public void justTesting(){
字符串url=”https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1";
HttpClient HttpClient=新的DefaultHttpClient();
HttpPut HttpPut=新的HttpPut(url);
httpput.addHeader(新的BasicHeader(“授权”、“谷歌登录验证=“+getAuthToken()));
addHeader(新的BasicHeader(“GData版本”,“3.0”);
httphe=null;
字符串messageBody=”http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1   ";
//RequestEntity RequestEntity=新的StringRequestEntity(xml.toString(),“application/atom+xml”,“UTF-8”);
试一试{
he=新的StringEntity(messageBody);
}
捕获(不支持的编码异常e){
Log.e(“错误”、“不支持的编码异常”);
}
addHeader(“内容类型”、“应用程序/atom+xml”);
httpput.setEntity(he);
试一试{
HttpResponse hr=httpclient.execute(httpput);
Log.d(“调试”,“sl:+hr.getStatusLine());
}
捕获(客户端协议例外e){
Log.e(“错误”、“客户端协议异常”);
}
捕获(IOE异常){
Log.e(“错误”、“IOException”);
}
Log.v(“测试”、“执行”);
}

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

嘿,我想起来了,我错过了这个:

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");
public void justTesting(){
字符串url=”http://spreadsheets.google.com/feeds/cells/t9VU1IwRrmG3h-nhI_J2fzg/od6/private/full/R2C1";
HttpClient HttpClient=新的DefaultHttpClient();
HttpPut HttpPut=新的HttpPut(url);
httpput.addHeader(新的BasicHeader(“授权”、“谷歌登录验证=“+getAuthToken()));
addHeader(新的BasicHeader(“GData版本”,“2.0”);
httpput.addHeader(新的BasicHeader(“如果匹配)”,“*”);
httphe=null;
字符串messageBody=”http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1   ";
试一试{
he=新的StringEntity(messageBody);
}
捕获(不支持的编码异常e){
Log.e(“错误”、“不支持的编码异常”);
}
httpput.addHeader(“内容类型”、“应用程序/a