【问题标题】:Feedly Mark as Read not working on Objective-CFeedly 标记为已读在 Objective-C 上不起作用
【发布时间】:2015-03-07 22:24:55
【问题描述】:

我正在尝试在我的 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 并且似乎无法弄清楚我做错了什么。我已经为此绞尽脑汁好几个小时了。

【问题讨论】:

  • 它需要一个 JSON 对象,因为您使用的是 POST。但是,您的数据似乎已针对 GET 调用进行了格式化。
  • 您是实际上以json格式提供输入,还是只是您的意见?看起来很像您将帖子数据重新格式化为 x-www-form-urlencoded...
  • 这只是我的意见。我不知道如何解决这个问题。

标签: ios json xcode feedly


【解决方案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&amp;type=entries&amp;entryIds=%@)。请查看POST vs GET 以获得更好的理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多