1

我正在创建一个显示我的 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 的新手。谢谢。

4

3 回答 3

3

与 api.twitter.com 的连接仅限于 TLS/SSL 连接。如果您的应用程序仍然使用 HTTP 纯文本连接,您需要更新它以使用 HTTPS 连接检查此链接:https ://dev.twitter.com/discussions/24239

于 2014-01-16T15:53:01.703 回答
0

Add the below code to get the full JSON string and know the reason behind your error:

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

Hope it helps.

于 2013-10-20T13:45:03.350 回答
0

您的代码正在调用2013 年 6 月 11 日正式停用的 Twitter API 版本 1 。您应该使用v1.1

于 2013-10-20T13:49:40.437 回答