在iOS的谷歌经纬度查询中没有得到json响应?

时间:2022-08-22 10:28:14

I am new to iOS, so if any help it will be appreciated. I am trying to get the longitude and latitude from address, earlier the code was working fine but now the JSON data are coming null.

我是iOS的新手,所以如果有任何帮助,我将非常感激。我试图从地址获取经度和纬度,之前代码运行良好,但现在JSON数据变为空。

Here my sample code,

在这里我的示例代码,

     url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false",appDelegate.sAddress];

    url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"Address URL: %@",url);

    //Formulate the string as a URL object.
    NSURL *requestURL=[NSURL URLWithString:url];
    NSData* data = [NSData dataWithContentsOfURL: requestURL];

    NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"my Coordinate : %@",returnString);

    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:data
                          options:kNilOptions 
                          error:&error];

But i am getting the output as null.

但我得到的输出是null。

So please help me out.

所以请帮帮我。

Thanks!

谢谢!

3 个解决方案

#1


1  

Thanks for your replies that all make me learn a lots. As one of my friend just tell me the solution so i am sharing with you.

谢谢你的回复,让我学到了很多。作为我的一个朋友,告诉我解决办法,这样我就可以和你们分享了。

Here is the code,

这是代码,

url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false",appDelegate.sAddress];

url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Address URL: %@",url);

//Formulate the string as a URL object.
NSURL *requestURL=[NSURL URLWithString:url];
NSData* data = [NSData dataWithContentsOfURL: requestURL];

NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *locationResult = [parser objectWithString:returnString];
//[reverseGeoString copy]`

And its working fine.

和它的工作好。

But still there is a question that why this happen.As earlier that code is working fine but it suddenly stopped working.

但仍有一个问题是为什么会发生这种情况。与前面一样,代码运行良好,但突然停止了工作。

#2


0  

You must construct your returnString in the following method that actually receives the data:

您必须使用以下方法构建您的returnString,该方法实际接收数据:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

Check out this for additional information on how to use NSURLConnection and the delegate methods.

有关如何使用NSURLConnection和委托方法的其他信息,请参阅本文。

#3


0  

I would say you're missing the all-important "REQUEST"...

我想说你错过了最重要的“请求”……

This is what I do. Hope it helps:

这就是我所做的。希望它可以帮助:

    NSString *encodedAddress = (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge_retained CFStringRef)searchBar.text, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 );

    NSString* searchURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",encodedAddress];
    NSError* error = nil;
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];

    NSURL* URL = [NSURL URLWithString:searchURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];

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

    if (error){
        NSLog(@"Error performing request %@", searchURL);
        return;
    }

    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (jsonString!=nil){
        NSLog(@"%@",jsonString);
    }

#1


1  

Thanks for your replies that all make me learn a lots. As one of my friend just tell me the solution so i am sharing with you.

谢谢你的回复,让我学到了很多。作为我的一个朋友,告诉我解决办法,这样我就可以和你们分享了。

Here is the code,

这是代码,

url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false",appDelegate.sAddress];

url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Address URL: %@",url);

//Formulate the string as a URL object.
NSURL *requestURL=[NSURL URLWithString:url];
NSData* data = [NSData dataWithContentsOfURL: requestURL];

NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *locationResult = [parser objectWithString:returnString];
//[reverseGeoString copy]`

And its working fine.

和它的工作好。

But still there is a question that why this happen.As earlier that code is working fine but it suddenly stopped working.

但仍有一个问题是为什么会发生这种情况。与前面一样,代码运行良好,但突然停止了工作。

#2


0  

You must construct your returnString in the following method that actually receives the data:

您必须使用以下方法构建您的returnString,该方法实际接收数据:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

Check out this for additional information on how to use NSURLConnection and the delegate methods.

有关如何使用NSURLConnection和委托方法的其他信息,请参阅本文。

#3


0  

I would say you're missing the all-important "REQUEST"...

我想说你错过了最重要的“请求”……

This is what I do. Hope it helps:

这就是我所做的。希望它可以帮助:

    NSString *encodedAddress = (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge_retained CFStringRef)searchBar.text, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8 );

    NSString* searchURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",encodedAddress];
    NSError* error = nil;
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];

    NSURL* URL = [NSURL URLWithString:searchURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];

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

    if (error){
        NSLog(@"Error performing request %@", searchURL);
        return;
    }

    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if (jsonString!=nil){
        NSLog(@"%@",jsonString);
    }