AFNetworking因response.statusCode 0而失败

时间:2015-03-10 08:37:24

标签: ios json afnetworking google-geocoder google-geocoding-api

我想通过GoogleGEO CODING(EX网址= http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false)来获取经度和纬度的地址

所以我想通过AFNetworking从该页面获取JSON。 这是我的代码:

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失败并且log显示0表示状态代码。

我也收到了我输入的url日志 我已经检查过网址没有问题(它显示json正常工作)

我用模拟器进行调试!

有什么我错过的吗?

0 个答案:

没有答案
相关问题