2013-07-27 38 views
1

我使用Google融合表的ios應用程序。在previous question我得到了建議並取得了進展,但仍然在創建融合表。我的代碼:從iOS創建Fusion表

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

看起來不錯。在你的代碼似乎缺少設置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]; 
+0

是做到了,謝謝! – asaki