首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Xcode / iOS中调用沃森人格特征API?

如何在Xcode / iOS中调用沃森人格特征API?
EN

Stack Overflow用户
提问于 2015-04-14 13:42:56
回答 1查看 216关注 0票数 0

我试图向https://gateway.watsonplatform.net/personality-insights/api/v2/profile发出一个POST请求,application/json作为Content-Type,下一个json作为body:

代码语言:javascript
复制
 {
   "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做这件事,这是我所得到的:

代码语言:javascript
复制
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字符串。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-14 13:57:21

在进行请求之前,您需要为请求的内容类型设置正确的AFHTTPRequestSerializer。您可以这样做:

代码语言:javascript
复制
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29629013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档