What goes wrong to access twitter timeline account?

时光毁灭记忆、已成空白 提交于 2020-01-01 06:55:36

问题


I tried the fallowing code to get access twitter timeline. It doesn't received any data from the server.What went wrong here?

 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");

Thanks in advance.


回答1:


Twitter has advice to use Version 1.1 not advice v1. In version 1.1 api https,So try to use this url https://api.twitter.com/1.1/statuses/user_timeline.json instated of this url http://api.twitter.com/1/statuses/user_timeline.json. This work's fine.




回答2:


Those NSError objects the API gives you? Their purpose is to tell you what went wrong. Use them.

Your problem is that you don't know what happened because you just try to convert to JSON. That is what could have went wrong:

  • the request failed (e.g. network problem)
  • you are not authorized to do whatever you did
  • the data returned is not actually JSON
  • the JSON object is not an array (would have lead to a crash).

To write defensive code (and that's what you want if you want to release this thing to the public) you have to check each of these steps to figure out what went wrong, so you can act accordingly. Yes, that will take more code, but less code is not always the best choice.

Code with better error handling would more look like this. Note how it checks the result of each step that could go wrong:

[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);
    }
}];


来源:https://stackoverflow.com/questions/21621056/what-goes-wrong-to-access-twitter-timeline-account

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!