ios - 如何在 Xcode/iOS 中调用 Watson Personality traits API?

标签 ios objective-c iphone ibm-watson personality-insights

我正在尝试使用 application/jsonhttps://gateway.watsonplatform.net/personality-insights/api/v2/profile 发出 POST 请求作为 Content-Type 和下一个 json 作为正文:

 {
   "contentItems" : [
     {
       "sourceid" : "twitter",
       "id" : "MYID",
       "userid" : "json",
       "language" : "en",
       "content" : "Call me Ishmael Some years ago-never mind how long precisely-having little or no money in my purse and nothing particular to interest me on shore I thought I would sail about a little and see the watery part of the world It is a way I have of driving off the spleen and regulating the circulation Whenever I find myself growing grim about the mouth whenever it is a damp drizzly November in my soul whenever I find myself involuntarily pausing before coffin warehouses and bringing up the rear of every funeral I meet and especially whenever my hypos get such an upper hand of me that it requires a strong moral principle to prevent me from deliberately stepping into the street and methodically knocking peoples hats off-then I account it high time to get to sea as soon as I can",
       "contenttype" : "text\/plain"
     }
   ]
 }

我试着用 AFNetworking 来做这个,这是我得到的:

NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"PI-USERNAME", @"PI-PASSWORD"];
 NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
 NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64Encoding]];

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
 [manager.requestSerializer setValue:authValue forHTTPHeaderField:@"Authorization"];

 NSString *sContent = @"Call me Ishmael Some years ago-never mind how long precisely-having little or no money in my purse and nothing particular to interest me on shore I thought I would sail about a little and see the watery part of the world It is a way I have of driving off the spleen and regulating the circulation Whenever I find myself growing grim about the mouth whenever it is a damp drizzly November in my soul whenever I find myself involuntarily pausing before coffin warehouses and bringing up the rear of every funeral I meet and especially whenever my hypos get such an upper hand of me that it requires a strong moral principle to prevent me from deliberately stepping into the street and methodically knocking peoples hats off-then I account it high time to get to sea as soon as I can";

 NSDictionary *myDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
                                    @"MYID", @"id",
                                    @"jason", @"userid",
                                    @"twitter", @"sourceid",
                                    @"text/plain", @"contenttype",
                                    @"en", @"language",
                                    sContent, @"content",
                                    nil];
 NSArray *myArray = [[NSArray alloc]initWithObjects:myDictionary, nil];
 NSDictionary *parameters = [[NSDictionary alloc]initWithObjectsAndKeys:myArray,@"contentItems", nil];

 [manager POST:@"https://gateway.watsonplatform.net/personality-insights/api/v2/profile" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"JSON: %@", responseObject);
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", error);
 }];

我认为它没有使用正确的序列化程序将我的 NSDictionary 编码为 json 字符串

最佳答案

在发出请求之前,您需要为请求的 Content-Type 设置正确的 AFHTTPRequestSerializer。您可以按如下方式执行此操作:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

关于ios - 如何在 Xcode/iOS 中调用 Watson Personality traits API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29629013/

相关文章:

iphone - 如何添加 NSDecimalNumbers?

iOS Objective-C AFNetworking dataTaskWithRequest :completionHandler: can't retrieve data from inside completition block

iphone - 分支超出范围编译错误

ios - 显示启动画面而不是快照

ios - 如何正确保存iOS应用状态?

ios - uitableviewcell 的图像正在拉伸(stretch)

ios - 关闭 SwiftUI 中的按钮突出显示

iphone - 如何将二进制文件上传到应用商店?

objective-c - 将字符串中的数据存储到整数数组中

ios - 将计时器标签格式化为小时 :minutes:seconds in Swift