views:

107

answers:

2

Hi, I'm trying to implement some basic functionality for Google Spreadsheets, using the protocol specification with requests.

The reason i'm doing this because it is for Android, and gdata-java library doesn't really work and the alpha android one doesn't really cut it. I managed to implement authentication, and get for lists, and delete, but for editing \ updating a row i can't really wrap my mind around it.

For example i want to change the contents of a cell here is the specification for the protocol

From my understanding of it, i have to send some sort of atom or xml formatted request to the edit URL. I never sent such type of requests.

I also found this, which gives a clue on how it should be done (also an unanswered question on a forum), but didn't really managed it.

Here is my authentication function if you need it, so you don't implement it again, if you want to try and code it.

/**
 * 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", "[email protected]"));
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;

}

Here is what i'm trying as the update function, note, edit URL's are for my document won't work if you try it, it's just for an iea on what i have so far :

/**
 * 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&lt;/id&gt;"+
                     "<link rel=\"edit\" type=\"application/atom+xml\""+
                     "  href=\"https://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1\"/&gt;"+
                     "<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");

}

This currently gives a 400 Bad request. Also I'm trying to achieve this using Apache HttpClient.

Does anyone have any idea on how this might be achieved \ implemented \ how what request i should send?

Thanks, your help will be greatly appreciated.

A: 

I made some progress and wrote this :

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'&gt; <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");

}

Now it gives me a 403 Forbidden. Any idea why?

theBlinker
A: 

Hey, I figured it out, i was missing this :

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

and now the function looks like this :

  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'&gt; <id>http://spreadsheets.google.com/feeds/cells/tl7RKkCaAxvO1f3U9Y8k5Dw/od6/private/full/R2C1&lt;/id&gt; <link rel='edit' type='application/atom+xml' href='http://spreadsheets.google.com/feeds/cells/t9VU1IwRrmG3h-nhI_J2fzg/od6/private/full/R2C1/1en5'/&gt; <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");

}

theBlinker