【发布时间】:2015-01-21 08:40:50
【问题描述】:
我正在尝试使用 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 中使用 HTTP 请求的可能反馈和最佳实践,因为我对这两者都是新手。
【问题讨论】:
-
看来输入我的问题解决了我的问题。我发布的是文件路径,而不是文件中的数据。我通过将帖子字符串更改为文件的实际内容来解决了这个问题。我之前尝试过这个,但是当我以 GET 而不是 POST 的形式发送请求时。
标签: ios http alchemyapi