2013-10-20 205 views
1

我正在創建一個顯示我的Twitter時間表的應用程序。但我有時會收到以下錯誤:iOS - Twitter連接問題:「操作無法完成(可可錯誤3840.)」

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.} 

我使用下面的代碼來檢索嘰嘰喳喳的時間表:

-(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]; 
        [email protected]"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的新手。 謝謝。

回答

0

添加下面的代碼來獲得完整的JSON字符串,並知道自己的錯誤背後的原因:

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

希望它能幫助。

+1

只是在您的代碼中進行了小幅更正:NSLog(@「%@」,newStr); – na19

+0

對不起,只是輸入錯誤:) – MuhammadBassio

+0

當我成功的結果Json打印正確。但問題是,我仍然得到間歇性的結果。我期待這種行爲的解釋。 – na19

0

您的代碼正在調用Twitter API的版本1,它是officially retired on June 11, 2013。你應該使用v1.1

+0

謝謝,所以從現在開始,我應該使用以下url:http://api.twitter.com/1.1/statuses/home_timeline.json。只是一個問題,爲什麼舊​​的網址也給我大部分時間的成功迴應? – na19

+1

這是因爲您正在調用API的非HTTPS版本。看起來Twitter還沒有把它關掉。所有Twitter API調用都應該使用v1.1和HTTPS。 – neilco

+0

我已經更新了Url。但這不是問題的根源,因爲我仍然得到間歇性的結果。 – na19

3

與api.twitter.com的連接僅限於TLS/SSL連接。如果您的應用程序仍然使用HTTP純文本連接,則需要將其更新爲使用HTTPS連接。 檢查此鏈接:https://dev.twitter.com/discussions/24239

相關問題