iOS-推特连接问题:“;操作无法’;不可能完成。(可可错误3840)。”;

iOS-推特连接问题:“;操作无法’;不可能完成。(可可错误3840)。”;,ios,objective-c,json,cocoa-touch,Ios,Objective C,Json,Cocoa Touch,我正在创建一个显示我的twitter时间线的应用程序。但我有时会出现以下错误: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1fd807c0 {NS

我正在创建一个显示我的twitter时间线的应用程序。但我有时会出现以下错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1fd807c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
我使用以下代码检索twitter的时间线:

-(void) getTimeLine{

ACAccountStore *account=[[ACAccountStore alloc] init];
ACAccountType *accountType=[account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[activityIndicatorLoadTweets setHidden:NO];
[activityIndicatorLoadTweets startAnimating];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
    if (granted==YES) {
        NSArray *arrOfAccounts=[account accountsWithAccountType:accountType];
        if ([arrOfAccounts count]!=0)
        {
            ACAccount *twitterAccount=[arrOfAccounts lastObject];
            NSURL *requestURL=[NSURL URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];
            NSMutableDictionary *params=[[NSMutableDictionary alloc] init];
            [params setObject:@"20" forKey:@"count"];
            [params setObject:@"1" forKey:@"include_entities"];

            SLRequest *postRequest=[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestURL parameters:params];

            postRequest.account=twitterAccount;

            [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
            {
                if (responseData==nil) {
                    NSLog(@"Could not connect. Try Again.");
                    [self showLabelAndStartTimer];
                    lblStatus.text=@"Could not connect at the moment. Please try Again.";
                    [activityIndicatorLoadTweets setHidden:YES];
                    [activityIndicatorLoadTweets stopAnimating];
                }
                else{
                    lblStatus.hidden=YES;
                    self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

                    if ([self.arrDataSource count]!=0)
                    {
                        dispatch_async(dispatch_get_main_queue(), ^
                        {
                            [self.tableTweetsView reloadData];
                            [activityIndicatorLoadTweets setHidden:YES];
                            [activityIndicatorLoadTweets stopAnimating];
                            //[self fetchBollyTweetsFrom:self.arrDataSource];
                        });
                    }
                }

            }];

        }
    }
    else{
        NSLog(@"Failure to access acount. Please check your internet connection.");
        [activityIndicatorLoadTweets setHidden:YES];
        [activityIndicatorLoadTweets stopAnimating];
    }


}];

}
请注意,错误描述来自以下行:

self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
这个错误基本上是不言自明的,但是如果我的JSON是错误的,为什么有一半的时间我得到了成功的响应? 欢迎对代码提出意见和建议,因为我是iOS编程和使用JSON的新手。
谢谢。

添加以下代码以获取完整的JSON字符串,并了解错误背后的原因:

NSString* newStr = [[NSString alloc] initWithData:responseData 
                                         encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr);
self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

希望它能有所帮助。

您的代码正在调用Twitter API的版本1,该版本已经发布。你应该使用

到api.twitter.com的连接仅限于TLS/SSL连接。如果应用程序仍然使用HTTP明文连接,则需要将其更新为使用HTTPS连接
检查此链接:

抱歉,只是输入错误:)当我获得成功结果时,Json打印正确。但问题是,我仍然得到断断续续的结果。我期待着对这种行为的解释。你能发布打印JSON的示例吗?谢谢,所以从现在起我应该使用以下url:。只是一个问题,为什么旧的url在大多数情况下也能给我成功的响应?这是因为你调用的是非HTTPS版本的API。Twitter似乎还没有关闭这个功能。所有Twitter API调用都应该使用v1.1和HTTPS。我已经更新了Url。但这不是问题的根源,因为我仍然得到间歇性的结果。