0

我有以下代码从 Twitter API 获取 twitter Id 和名称:

{

        NSString *StringUrl = [NSString stringWithFormat:@"https://api.twitter.com/1/users/show.json?screen_name=%@",account.username];

        @try {

                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [request setHTTPMethod:@"GET"];
                [request setTimeoutInterval: 15];
                [request setURL:[NSURL URLWithString:StringUrl]];

                NSError *error = [[NSError alloc] init];
                NSHTTPURLResponse *responseCode = nil;

                NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

                if([responseCode statusCode] != 200){
                    NSLog(@"Error getting %@, HTTP status code %i", StringUrl, [responseCode statusCode]);
                }else{
                    NSLog(@"Good");
                    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                    NSString *twitterId = [json objectForKey:@"id_str"];
                    NSString *twitterName = [json objectForKey:@"name"];
                }

        }
        @catch (NSException *exception) {
            NSLog(@"No conection: %@",exception.name);
        }
}

通过 wifi 网络测试时效果很好;但是通过蜂窝网络,它会响应:

Error getting https://api.twitter.com/1/users/show.json?screen_name=enf_4eva, HTTP status code 400

提前致谢。

4

1 回答 1

0

似乎这不是执行此类请求的最佳方式,因为请求必须通过 3G 进行身份验证。使用新的 SLRequest,我可以设置执行请求的帐户,使其运行良好。

于 2012-09-28T20:34:10.367 回答