【问题标题】:What goes wrong to access twitter timeline account?访问 Twitter 时间线帐户出了什么问题?
【发布时间】:2014-02-08 15:42:12
【问题描述】:

我尝试了下面的代码来访问 Twitter 时间线。它没有从服务器收到任何数据。这里出了什么问题?

 ACAccount *twitterAccount=[arrayOfAccounts lastObject];

            NSURL *requestURL=[NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"];

            NSMutableDictionary *parameters=[NSMutableDictionary new];
            //[parameters setObject:@"100" forKey:@"count"];
            //[parameters setObject:@"1" forKey:@"include_entities"];

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

            post.account=twitterAccount;

            [post performRequestWithHandler:^(NSData *response, NSHTTPURLResponse *urlResponse, NSError *error) {

                self.array=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
                if(self.array.count !=0)
                    NSLog(@"%@",self.array);
                else
                    NSLog(@"No Data Recived");

提前致谢。

【问题讨论】:

    标签: ios twitter social


    【解决方案1】:

    Twitter 建议使用版本 1.1 而不是建议 v1。在 1.1 版 api https 中,因此尝试使用此 url https://api.twitter.com/1.1/statuses/user_timeline.json 替换此 url http://api.twitter.com/1/statuses/user_timeline.json。这项工作很好。

    【讨论】:

      【解决方案2】:

      API 为您提供的那些NSError 对象?他们的目的是告诉你出了什么问题。使用它们。

      您的问题是您不知道发生了什么,因为您只是尝试转换为 JSON。这就是可能出错的地方:

      • 请求失败(例如网络问题)
      • 您无权做任何事情
      • 返回的数据实际上不是 JSON
      • JSON 对象不是数组(会导致崩溃)。

      要编写防御性代码(如果您想向公众发布这个东西,这就是您想要的),您必须检查每个步骤以找出问题所在,以便采取相应措施。是的,这需要更多的代码,但更少的代码并不总是最好的选择。

      具有更好错误处理的代码看起来更像这样。请注意它如何检查可能出错的每个步骤的结果:

      [post performRequestWithHandler:^(NSData *response, NSHTTPURLResponse *urlResponse, NSError *error) {
          if (response) {
              // TODO: might want to check urlResponse.statusCode to stop early
              NSError *jsonError;  // use new instance here, you don't want to overwrite the error you got from the SLRequest
              NSArray *array =[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&jsonError];
              if (array) {
                  if ([array isKindOfClass:[NSArray class]]) {
                      self.array = array;
                      NSLog(@"resulted array: %@",self.array);
                  }
                  else {
                      // This should never happen
                      NSLog(@"Not an array! %@ - %@", NSStringFromClass([array class]), array);
                  }
              }
              else {
                  // TODO: Handle error in release version, don't just dump out this information
                  NSLog(@"JSON Error %@", jsonError);
                  NSString *dataString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
                  NSLog(@"Received data: %@", dataString ? dataString : response);    // print string representation if response is a string, or print the raw data object
              }
          }
          else {
              // TODO: show error information to user if request failed
              NSLog(@"request failed %@", error);
          }
      }];
      

      【讨论】:

        猜你喜欢
        • 2012-06-27
        • 1970-01-01
        • 2015-06-06
        • 2015-09-09
        • 1970-01-01
        • 2013-08-07
        • 2014-01-18
        • 2011-11-10
        • 2015-07-30
        相关资源
        最近更新 更多