Ios AFN网络连接失败,响应为。状态代码0
我想通过谷歌地理编码(EX URL=)从纬度和经度获取地址 所以我想通过AFNetworking从该页面获取JSON。 下面是我的代码:Ios AFN网络连接失败,响应为。状态代码0,ios,json,afnetworking,google-geocoder,google-geocoding-api,Ios,Json,Afnetworking,Google Geocoder,Google Geocoding Api,我想通过谷歌地理编码(EX URL=)从纬度和经度获取地址 所以我想通过AFNetworking从该页面获取JSON。 下面是我的代码: IBAction)reLocation:(UIButton*)sender { if(sender.tag==1) { NSArray *gpsValue = [self getGPS]; float lat = [[gpsValue objectAtIndex:0] floatValue]; float lo
IBAction)reLocation:(UIButton*)sender
{
if(sender.tag==1)
{
NSArray *gpsValue = [self getGPS];
float lat = [[gpsValue objectAtIndex:0] floatValue];
float lon = [[gpsValue objectAtIndex:1] floatValue];
NSString *string = [NSString stringWithFormat:@"%@%@,%@&sensor=true_or_false",GEOCODING_URL,[NSString stringWithFormat:@"%f", lat],[NSString stringWithFormat:@"%f",lon]]; // NSString *str = [NSString stringWithFormat:@"%f", myFloat];
NSLog(string);
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"AFNetworking success");
NSDictionary *location = (NSDictionary *)responseObject;
// 3
self.title = @"JSON Retrieved";
//[self.tableView reloadData];
NSLog(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"AFNetworking failure");
switch (operation.response.statusCode) {
case 400:
// Do stuff
NSLog(@"error 400");
break;
default:
NSLog([NSString stringWithFormat:@"%ld",(long)operation.response.statusCode]);
break;
}
// 4
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
// 5
[operation start];
}}
但当我点击我的按钮时,afnetworking总是失败,日志显示状态代码为0
我还得到了我放进去的url日志
我已经检查过url并没有问题(它以工作顺序显示json)
我用模拟器调试
是否有遗漏?有时模拟器连接丢失,请重新启动模拟器。