从iOS创建Fusion Table

时间:2013-07-27 08:49:36

标签: ios objective-c google-fusion-tables

我在使用Google融合表的ios应用程序上工作。在previous question我获得了建议并取得了进展,但仍然在努力创建融合表。我的代码:

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];

但我收到错误:“操作无法完成。(com.google.HTTPStatus错误400.)”。我做错了什么?

1 个答案:

答案 0 :(得分:1)

看起来是一个很好的进步。在您的代码中,您似乎缺少设置http标头内容类型,应该是:

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

另外,在执行GTMHTTPFetcher错误处理时,您可能会使用以下内容获取更多信息:

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