我正在尝试使用带有请求的协议规范来为 Google 电子表格实现一些基本功能。
我这样做的原因是因为它适用于 Android,而 gdata-java 库并不能真正工作,而 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;
}
这是我正在尝试的更新功能,请注意,如果您尝试它,我的文档的编辑 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。知道为什么吗?