我正在尝试为Google Spreadsheets实现一些基本功能, 将协议规范与请求一起使用。
我这样做的原因是因为它适用于Android,并且gdata-java库并不真正起作用,而且alpha安卓程序并没有真正削减它。 我设法实现身份验证,获取列表和删除,但是对于编辑\更新行,我无法真正理解它。
例如,我想更改单元格的内容 here是协议的规范
根据我对它的理解,我必须向编辑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;
}
以下是我正在尝试的更新功能,请注意,如果您尝试使用,我的文档的编辑网址将不起作用,它只是针对我目前所拥有的内容:
/**
* 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。知道为什么吗?
答案 0 :(得分:1)
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");
}