我正在尝试使用API调用TextGetRankedNamedEntities http://www.alchemyapi.com/api/entity/textc.html#rtext
以下是我的相关代码:
NSString *data = @"Data I want analyzed";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"result.txt"];
[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
//I have verified the data is in the file
NSString *queryString = [NSString stringWithFormat:@"http://access.alchemyapi.com/calls/text/TextGetRankedNamedEntities?apikey=MY_API_KEY&showSourceText=1"];
NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:[
NSURL URLWithString: queryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
//Set request method to post
[theRequest setHTTPMethod:@"POST"];
NSString *post = [NSString stringWithFormat:@"&text=%@", appFile];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
[theRequest setHTTPBody:postData];
此请求的结果是:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<status>OK</status>
<usage>By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html</usage>
<url></url>
<language>english</language>
<text>/var/mobile/Containers/Data/Application/6C00A4F8-4DEE-44F2-9DBF-7568CEF72054/Documents/result.txt</text>
<entities>
</entities>
</results>
似乎正在发送的数据是文件路径,而不是文件的实际内容。
编辑:已解决,留下关于在iOS中使用HTTTP请求的可能反馈和最佳做法的问题,因为我对两者都不熟悉。
答案 0 :(得分:0)
变化:
NSString *post = [NSString stringWithFormat:@"&text=%@", appFile];
要:
NSString *post = [NSString stringWithFormat:@"&text=%@", data];
在发布问题之前我没有想过尝试这个,因为我之前尝试过这样做,但此时我发送的请求是使用GET方法而不是POST方法。