Search code examples
iosobjective-cgoogle-fusion-tables

creating Fusion Table from iOS


I work on ios application that use Google fusion tables. in previous question I got advice and made progress but still struggle with creating fusion table. my code:

NSString *url = @"https://www.googleapis.com/fusiontables/v1/tables";

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myTableDict options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonDataString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *postData = [jsonDataString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[fetcher setAuthorizer:self.auth];
[fetcher setPostData:postData];
void(^createHandler)(NSData *data, NSError *error) = ^(NSData *data, NSError *error){
    if (error) {
        NSLog(@"error creating table %@", [error localizedDescription]);
    }
};
[fetcher beginFetchWithCompletionHandler:createHandler];

But I am getting error: "The operation couldn’t be completed. (com.google.HTTPStatus error 400.)". what am I doing wrong?


Solution

  • looks like a good progress. in your code you seem to be missing setting the http header content type, should be:

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
    

    also, when doing GTMHTTPFetcher error processing you might get more info using:

    NSData *data = [[error userInfo] valueForKey:@"data"];
    NSString *errorDataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];