2014-03-12 40 views
0

我正在嘗試執行三種不同的JSON連接來使用三個不同的MySQL表格標記填充mapView。JSON連接在地圖上顯示標記查看

這是我現在要做的代碼。爲了測試連接,我正在記錄didReceiveData方法,但編譯器光標沒有到達該方法,當然,任何標記都顯示在地圖上。

您懇請看看我的代碼,並告訴我我錯過了什麼或做錯了什麼。謝謝。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"Geolocalización"; 


    //carga de datos JSON 
    //network activity indicator showed during downloading data 
    //PROCESSING FIRST CONNECTION 
    first_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"]; 
    NSURLRequest *first_connection_request = [NSURLRequest requestWithURL:first_connection_url]; 
    NSURLConnection *first_connection=[[NSURLConnection alloc]initWithRequest:first_connection_request delegate:self]; 

    //PROCESSING SECOND CONNECTION 
    second_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/centrostodoslist.php"]; 
    NSURLRequest *second_connection_request = [NSURLRequest requestWithURL:second_connection_url]; 
    NSURLConnection *second_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self]; 
    //PROCESSING THIRD CONNECTION 
    third_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/eventostodoslist.php"]; 
    NSURLRequest *thrid_connection_request = [NSURLRequest requestWithURL:third_connection_url]; 
    NSURLConnection *third_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self]; 


} 

//methods to perform the connection and population of data 

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if(connection==first_connection){ 
     data_for_first_connection = [[NSMutableData alloc]init]; 

    } 
    else if(connection==second_connection){ 
     data_for_second_connection = [[NSMutableData alloc]init]; 
    } 
    else if(connection==third_connection){ 
     data_for_third_connection = [[NSMutableData alloc]init]; 
    } 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata 
{ 
    if(connection==first_connection){ 
     [data_for_first_connection appendData:thedata]; 
     NSLog(@"CONEXION 1 AQUI"); 
    } 
    else if(connection==second_connection){ 
     [data_for_second_connection appendData:thedata]; 
    NSLog(@"CONEXION 2 AQUI"); 
    } 
    else if(connection==third_connection){ 
     [data_for_third_connection appendData:thedata]; 
    NSLog(@"CONEXION 3 AQUI"); 
    } 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    //if data received network indicator not visible 
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO; 

    if(connection==first_connection) { 

     categorias_first = [NSJSONSerialization JSONObjectWithData:data_for_first_connection options:0 error:nil]; 

    } 
    else if(connection==second_connection){ 

     categorias_second = [NSJSONSerialization JSONObjectWithData:data_for_second_connection options:0 error:nil]; 
    } 
    else if(connection==third_connection){ 
     // PROCESS TO BE DONE FOR SECOND CONNECTION 
     categorias_third = [NSJSONSerialization JSONObjectWithData:data_for_third_connection options:0 error:nil]; 
    } 

    // Create a GMSCameraPosition that tells the map to display the 
    // coordinate -33.86,151.20 at zoom level 6. 
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:27.9847 
                  longitude:-15.5953 
                   zoom:9]; 
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView_.myLocationEnabled = YES; 
    mapView_.delegate = self; 
    self.view = mapView_; 

    UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Ofertas", @"Cursos", @"Eventos", nil]]; 
    [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar]; 
    mainSegment.frame = CGRectMake(20,80, 280, 43); 
    //self.navigationItem.titleView = mainSegment; 
    //mainSegment.selectedSegmentIndex = nil; 
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged]; 
    [self.view addSubview:mainSegment]; 


} 

更新的測試應用程序的代碼。

我已更新我的代碼來測試它,現在我只使用兩個連接。在記錄結果,我發現,數組categoria(在first_connection)和categoria2(在second_connection)是由相同的JSON文件(從first_connection)填充:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"Geolocalización"; 



    //carga de datos JSON 
    //network activity indicator showed during downloading data 
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES; 

    //URL definition where php file is hosted 
    //conexion1 
    url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"]; 

    request = [NSURLRequest requestWithURL:url]; 

    first_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


    //conexion2 
    url2 = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/centrostodoslist.php"]; 

    request2 = [NSURLRequest requestWithURL:url]; 

    second_connection = [[NSURLConnection alloc] initWithRequest:request2 delegate:self]; 






} 

//methods to perform the connection and population of data 

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

    if ([connection isEqual:first_connection]){ 
     NSLog(@"FIRST CONNECTION RESPONSE"); 
     data = [[NSMutableData alloc]init]; 
    } 

    if ([connection isEqual:second_connection]){ 
     NSLog(@"SECOND CONNECTION RESPONSE"); 
     data2 = [[NSMutableData alloc]init]; 
    } 


    NSLog(@"ESTOY EN DIDRECEIVERESPONSE"); 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata 
{ 


    if ([connection isEqual:first_connection]){ 
     NSLog(@"FIRST CONNECTION RECEIVED"); 
     [data appendData:thedata]; 

    } 
    if ([connection isEqual:second_connection]){ 
     NSLog(@"SECOND CONNECTION RECEIVED"); 
     [data2 appendData:thedata]; 

    } 




} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    //if data received network indicator not visible 
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO; 

    //array waterfalls populated via JSON from database 

    if ([connection isEqual:first_connection]){ 
    categorias = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
     NSLog(@"DATA1 = %@",categorias); 
    } 
    if ([connection isEqual:second_connection]){ 
    categorias2 = [NSJSONSerialization JSONObjectWithData:data2 options:0 error:nil]; 
     NSLog(@"DATA2 = %@",categorias2); 
    } 
    NSLog(@"NUMERO DE EMPRESAS OPCION1= %lu" 
      , (unsigned long)[categorias count]); 
    NSLog(@"NUMERO DE EMPRESAS OPCION2= %lu" 
      , (unsigned long)[categorias2 count]); 
    // Create a GMSCameraPosition that tells the map to display the 
    // coordinate -33.86,151.20 at zoom level 6. 
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:27.9847 
                  longitude:-15.5953 
                   zoom:9]; 
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView_.myLocationEnabled = YES; 
    mapView_.delegate = self; 
    self.view = mapView_; 




    UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Ofertas", @"Cursos", @"Eventos", nil]]; 
    [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar]; 
    mainSegment.frame = CGRectMake(20,80, 280, 43); 
    //self.navigationItem.titleView = mainSegment; 
    //mainSegment.selectedSegmentIndex = nil; 
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged]; 
    [self.view addSubview:mainSegment]; 


} 
+0

你可以嘗試使用'connectionWithRequest'而不是'initWithRequest'。 – user2071152

+0

謝謝@ user2071152,我現在就試試,但我想這不是問題。首先,我嘗試了只有一個連接,它工作正常。 – mvasco

+0

@ user2071152,您的提議會引發編譯器錯誤。 – mvasco

回答

3

我看到一些問題。首先,你的連接,firstConnection,secondConnection等被創建爲局部變量,所以只要viewDidLoad超出範圍,它們就會被釋放。當你在委託方法中檢查平等時,他們在那時將是零。您應該爲這些連接創建ivars或屬性。此外,您不應該使用「==」來比較對象,而是使用isEqual:。

else if([connection isEqual:third_connection]){ 
+0

謝謝,我會嘗試使用ivars並更改== isEqual ... – mvasco

0

你是不是叫「開始」爲您的連接。

1)爲了保持你的代碼,你需要在initWithRequest:;之後爲每個連接調用「start」。
例如,[first_connection start];

2)如果你需要的(這似乎是如此)是做一個HTTP GET並獲得NSData的響應,你可以簡單地做NSData *jsonData = [NSData dataWithContentsOfUrl:[NSURL urlWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"]];

3)如果你需要在一個單獨的線程發生,你可以調用dataWithContentsOfUrl塊內部是這樣的:

<pre> 
- (void) getJsonData 
{ 
    //main thread 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
    ^{ 
     //background thread 
     NSData *jsonData = [NSData dataWithContentsOfUrl:[NSURL urlWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"]]; 
     dispatch_async(dispatch_get_main_queue(), 
     ^{ 
      //main thread again 

      //parse json and add pins 
     }); 
    }); 
} 
</pre> 

祝你好運!

+1

點1不正確。在使用方法initWithRequest:delegate:時,不需要調用「start」。正如文檔所述,「返回一個初始化的URL連接並開始加載URL請求的數據」。 – rdelmar

1

眼前的問題,如rdelmar指出,是,你不保存在實例變量,你指以後NSURLConnection引用。這是第一個要解決的問題。

更廣泛的問題更根本。你開始建立三個連接,但你的connectionDidFinishLoading似乎沒有認識到它會被調用三次,每次連接一次。值得注意的是,該例程的後半部分(即創建地圖視圖和段控制)將針對三個JSON下載請求中的每一個執行一次。當所有三個連接完成時,你當然只想做一次與UI相關的東西。

優雅的方法是將這些單獨的網絡請求包裝在NSOperation對象中,並使用操作隊列來管理包含UI內容的「完成邏輯」。你可以通過AFNetworking來實現,或者將NSURLConnection包裝在你自己的NSOperation子類中。然後你可以添加這三個操作,但有一個最終完成操作,取決於其他三個下載操作的完成情況,這將更新UI。

對現有代碼更簡單的修復方法是添加三個布爾型ivars,指示是否完成三個單獨的下載。然後你的connectionDidFinishLoading,因爲它每次爲三個下載請求調用,可以更新各自的布爾值。如果所有三個都完成,它將只執行UI相關的東西。

+0

謝謝,我已經更改了我的代碼以實現rdelmar指出的內容。要測試它,我只使用兩個連接。現在我正在記錄方法connectionDidFinishLoading,並檢測到兩個連接。在didReceiveData方法中,我在每個連接處填充一個數組,但是記錄該方法我檢測到兩個數組都由第一個連接填充。 – mvasco

+0

@mvasco您應該編輯您的問題,並在問題結尾添加代碼的最新版本。我們不可能在沒有看到代碼的情況下進行診斷。 – Rob

+0

好的,謝謝,我現在將編輯我的問題以顯示最新更新。 – mvasco