我有以下代码从 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
提前致谢。