在 for 循环中使用不同的 url 多次发送 NSURL 请求



我有一个地址数组,需要使用谷歌的Geocode api将其转换为Lat/Long。我正在将地址和城市输入谷歌地理代码URL,这形成了一个正确的连接URL。

基本上,我希望能够使用for循环来创建多个NSURLConnection请求,并返回多个响应。

-(void)setString{
 for (int i = 0; i < [businessArray count]; i ++)
{         
NSString *address = [addressArray objectAtIndex:0];
NSString *city = [locationDict valueForKey:@"city"];
NSString *geocodeURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@,+%@,&sensor=true", address, city];
    geocodeURL = [geocodeURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:geocodeURL]
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:10];
    NSLog(@"%@", request);
    geoCodeConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

    if (geoCodeConnection)
    {
        responseData = [NSMutableData data];
        connectionIsActive = YES;
        NSLog(@"connection active");
    } else {
        NSLog(@"connection failed");
        connectionIsActive = NO;
    }
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *responseString    = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSError *jsonError          = nil;
    SBJsonParser *json          = [[SBJsonParser alloc] init];
    NSDictionary *parsedJSON    = [json objectWithString:responseString error:&jsonError];
    NSString *lat= [[[[parsedJSON valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"];
    NSString *lng= [[[[parsedJSON valueForKey:@"results"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"];
    NSLog(@"lat = %@ long= %@", lat, lng);
    connectionIsActive          = NO;
    [geoCodeLatArray addObject:lat];
    [geoCodeLngArray addObject:lng];
    NSLog(@"geoCodeArrayLat: %@", geoCodeLatArray);

}

现在,代码只返回最后一个地址"lat and long"。如何使用JSON发送multiply请求并返回multiply响应?

试试这个我正在使用这个,

for(int i=0;i< businessArray.count;i++)
{
    NSString *address = [addressArray objectAtIndex:i];
    NSString *city = [locationDict valueForKey:@"city"];
    NSString *address = [NSString stringWithFormat:@"%@,%@", address, city];
    CLLocationCoordinate2D location = [self geoCodeUsingAddress:address];
    // then here store the location.latitude in lat array and location.longitude in long array.
}
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];
    NSDictionary    *resultsDict = [googleResponse valueForKey:  @"results"];
    NSDictionary   *geometryDict = [resultsDict valueForKey: @"geometry"];
    NSDictionary   *locationDict = [geometryDict valueForKey: @"location"];
    NSArray *latArray = [locationDict valueForKey: @"lat"];
    NSString *latString = [latArray lastObject];
    NSArray *lngArray = [locationDict valueForKey: @"lng"];
    NSString *lngString = [lngArray lastObject];
    CLLocationCoordinate2D location;
    location.latitude = [latString doubleValue];
    location.longitude = [lngString doubleValue];
    return location;
}

更新上述功能:

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@""lat" :" intoString:nil] && [scanner scanString:@""lat" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@""lng" :" intoString:nil] && [scanner scanString:@""lng" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;
    return location;
}

这对我很有效。

您可以使用异步方法来解决这个问题,该方法执行请求并具有一个完成块,当结果可用时将调用该完成块。此完成块提供了一个参数结果,它是连接请求的结果。

这种方法可以声明如下:

typedef void (^completion_block_t) (id result);
- (void) fetchGeoCoordinateForAddress:(NSString*)address 
                    completionHandler:(completion_block_t)completionHandler;

例如,如果请求成功,则块中的参数结果是响应数据的JSON表示。否则,result是指示错误的NSError对象。但具体细节取决于如何实现方法fetchGeoCoordinateForAddress:completionHandler:

现在您可以按如下方式设置循环:

for (NSString* address in addresses) 
{
    [self fetchGeoCoordinateForAddress:address completionHandler:^(id result) {
        if (![result isKindOfError:[NSError class]]) // check if result is an error
        {
            // Note: result is not nil and is a NSDictionary representation of JSON.
            // Retrieve the "location" from the response:
            NSDictionary* location = result[@"results"][@"geometry"][@"location"];
            // Multiple request can occur at the same time! Thus, we need to 
            // synchronize access to the result array "myLocations" through 
            // accessing it *exclusively and everywhere* on the main thread:
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.myLocations addObject:location];
            });                
        }
        else {
            // got error
            DebugLog(@"ERROR: %@", result);
        }
    }
}

注意:根据实际的JSON和其他细节,您的实际代码可能略有不同。

关于方法fetchGeoCoordinateForAddress:completionHandler:的实现,您有几个选项:

  1. 使用第三方库并实现一个简单的方便包装器fetchGeoCoordinateForAddress:completionHandler:

  2. 创建您自己的"MyHTTPConnectionOperation"类,该类将NSURLConnection和响应数据以及其他一些有用的状态信息封装在一个专用类中。这个类通过start方法异步执行请求,并有一个完成处理程序。基本上,所有第三方网络图书馆都将使用这种方法。然后实现包装器。

  3. 如果NSURLConnection的异步方便方法足够并且适用于您的上下文,请使用它。这是实现最快但最不灵活的方法,可能不适用于所有情况,也可能只适用于次优情况。

编辑:

几个提示:

  • 如果可能,请使用NSJSONSerialization解析JSON并创建Foundation表示。其他第三方库只有在您有特殊要求时才提供一点优势,例如,您需要"使用NSData对象进行分块解析"——当您希望同时下载和解析时,这很有用。或者您需要创建除Foundation之外的其他表示,比如C++容器,或者您希望直接创建具有SAX风格解析的Model。或者,您需要更好的性能和更低的内存食物打印,因为您正在接收要保存到磁盘的超大字符串。NSJSONSerialization最近变得相当快,所以今天不应该只讨论"性能"。

  • 请求的超时时间不应低至10秒。在蜂窝连接中,这太少了。将其保留为默认值。

  • 如果您计划实现自己的"HTTPConnectionOperation"类,我在这里放了一个非常有限的示例,它可以让您快速入门。

我认为您必须从AFNetworking 开始

AF网络1
AFNetworking2

因为AFNetworking在调度和排队请求以及暂停和取消请求方面提供了很大的能力和灵活性。

最新更新