我在使用Microsoft Emotion API时遇到问题我已阅读documentation但无法使用它。每当我使用下面的代码时,它会给出Bad body JSON解析错误。我无法检测代码中的问题是什么。
我创建了一个方法 - (IBAction)clickToGenerateEmotion:(id)sender
NSString* path = @"https://api.projectoxford.ai/emotion/v1.0/recognize";
NSArray* array = @[
@"entities=true",
];
NSString* string = [array componentsJoinedByString:@"&"];
path = [path stringByAppendingFormat:@"?%@", string];
NSLog(@"%@", path);
UIImage *yourImage= _image;
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:@"POST"];
[_request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[_request setValue:@"9b118d1587ce40899598b48a6c29b51a" forHTTPHeaderField:@"Ocp-Apim-Subscription-Key"];
NSDictionary *params = @{@"\"url\"" : @"\"http://engineering.unl.edu/images/staff/Kayla_Person-small.jpg\""};
NSMutableArray *pairs = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in params) {
[pairs addObject:[NSString stringWithFormat:@"%@=%@", key, params[key]]];
}
NSString *requestParams = [pairs componentsJoinedByString:@"&"];
[_request setHTTPBody:imageData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];
NSLog(@"responseData = %@", [[NSString alloc] initWithData:_connectionData encoding:NSUTF8StringEncoding]);
if (nil != error)
{
NSLog(@"Error: %@", error);
}
else
{
NSError* error = nil;
NSMutableDictionary* json = nil;
NSString* dataString = [[NSString alloc] initWithData:_connectionData encoding:NSUTF8StringEncoding];
NSLog(@"%@", dataString);
if (nil != _connectionData)
{
json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
}
if (error || !json)
{
NSLog(@"Could not parse loaded json with error:%@", error);
}
NSLog(@"%@", json);
_connectionData = nil;
}
`