【问题标题】:Post Request to alchemy API in iOS8在 iOS8 中向炼金术 API 发布请求
【发布时间】: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


【解决方案1】:

变化:

NSString *post = [NSString stringWithFormat:@"&text=%@", appFile];

收件人:

NSString *post = [NSString stringWithFormat:@"&text=%@", data];

在发布问题之前我没有想到要尝试这个,因为我之前曾尝试过这样做,但那时我使用 GET 方法而不是 POST 方法发送请求。

【讨论】:

    猜你喜欢
    • 2016-09-27
    • 1970-01-01
    • 2016-06-21
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多