Microsoft Emotion API の使用に問題があります。ドキュメントを読みましたが、使用できません。以下のコードを使用するたびに、Bad body JSON parsing error が発生します。コードの問題を検出できません。
メソッドを作成しました - (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;
}
`