Feedly Mark as Read不能在Objective-C上工作

时间:2015-01-09 14:54:27

标签: ios json xcode feedly

我尝试在我的iOS应用中实现一种方法,使用Feedly API将文章标记为已读。但是,我一直收到错误回复:

{
    "errorCode": 400,
    "errorId": "sandbox-he.2015010906.2770040",
    "errorMessage": "no data"
}

这是方法:

- (void)markRead:(NSString*)articleID {
    NSLog(@"Artidle ID is: %@", articleID);

    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *accessToken = [standardUserDefaults objectForKey:@"AccessToken"];

    NSString *feedUrl = [NSURL URLWithString:@"https://sandbox.feedly.com/v3/markers"];

    NSError *error = nil;

    NSString *post =[[NSString alloc] initWithFormat:@"action=markAsRead&type=entries&entryIds=%@",articleID];
    NSLog(@"PostData: %@",post);

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:feedUrl];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
    [request setHTTPBody:postData];
    [request addValue:accessToken forHTTPHeaderField:@"Authorization"];

    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

    NSError *errror = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&errror];

    NSLog(@"Response code: %ld", (long)[response statusCode]);

    if ([response statusCode] >= 200 && [response statusCode] < 300)
    {
        NSLog(@"Success marking this as read.");

    } else {
        if (error) NSLog(@"Error: %@", errror);
        NSLog(@"No success marking this as read.");
    }

}

Feedly API描述了它需要输入:

{
   "entryIds": [
       "TSxGHgRh4oAiHxRU9TgPrpYvYVBPjipkmUVSHGYCTY0=_14499073085:c034:d32dab1f",
       "TSxGHgRh4oAiHxRU9TgPrpYvYVBPjipkmUVSHGYCTY0=_1449255d60a:22c3491:9c6d71ab"
   ],
   "action": "markAsRead",
   "type": "entries"
}

在我看来,我提供这个作为输入,所以我实际上期望得到适当的回应。但是,我一直得到400并且似​​乎无法弄清楚我做错了什么。我已经在这个问题上花了好几个小时。

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码创建数据,而不是使用当前的正文数据:

NSDictionary *bodyDict = [NSDictionary dictionaryWithObjectsAndKeys:@[articleID],@"entryIds",
                          @"markAsRead",@"action",
                          @"entries",@"type", nil];

NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:bodyDict options:0 error:&error];
[request setHTTPBody:postData];

这会将正文作为JSON对象发送。

在使用GET请求方法调用Web方法时,通常会使用与您所做的一样的发送参数(使用action=markAsRead&type=entries&entryIds=%@)。请查看POST vs GET以获得更好的理解。