[英]iOS - Twitter Connectivity Issue: “The operation couldn’t be completed. (Cocoa error 3840.)”
I am creating an app displaying my twitter timeline. 我正在创建一个显示我的Twitter时间线的应用程序。 But I sometimes get the following error:
但我有时会收到以下错误:
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.}
I am using the following code to retrieve the timeline of twitter: 我使用以下代码来检索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];
}
}];
}
Please note that the error description is from the line: 请注意,错误描述来自以下行:
self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
The error is mostly self explanatory but If my JSON is wrong, why I am getting successful response half of the time? 错误主要是自我解释但如果我的JSON错误,为什么我一半的时间得到成功的响应? Comments and suggestions on the code are also welcome as I am newbie in iOS programming and using JSON.
对于代码的评论和建议也很受欢迎,因为我是iOS编程和使用JSON的新手。 Thanks.
谢谢。
connections to api.twitter.com has been restricted to TLS/SSL connections only. 与api.twitter.com的连接仅限于TLS / SSL连接。 If your application still uses HTTP plaintext connections you will need to update it to use HTTPS connections check this link : https://dev.twitter.com/discussions/24239
如果您的应用程序仍使用HTTP纯文本连接,则需要将其更新为使用HTTPS连接,请检查以下链接: https : //dev.twitter.com/discussions/24239
Add the below code to get the full JSON string and know the reason behind your error: 添加以下代码以获取完整的JSON字符串并了解错误背后的原因:
NSString* newStr = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr);
self.arrDataSource=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
Hope it helps. 希望能帮助到你。
Your code is calling version 1 of the Twitter API which was officially retired on June 11, 2013 . 您的代码正在调用Twitter API的版本1,该版本已于2013年6月11日正式停用 。 You should be using v1.1 .
你应该使用v1.1 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.