1

我在使用 Google 融合表的 ios 应用程序上工作。在上一个问题中,我得到了建议并取得了进展,但仍在努力创建融合表。我的代码:

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。)”。我究竟做错了什么?

4

1 回答 1

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];
于 2013-07-27T08:57:46.423 回答