2014-07-08 47 views
0

我需要從四個URL中分別獲取單個JSON數據值。我已經寫擺脫單一的URL JSON數據的代碼運行多個網址以獲取多個JSON值

NSString *url=[NSString stringWithFormat:@"http://jsondata.in/UserImage.svc/GetUnAnsweredImagesLogCount?UserId=%@",requestString]; 


     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
     [request setURL:[NSURL URLWithString:url]]; 
     [request setHTTPMethod:@"GET"]; 

     NSURLConnection * connReq = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

     if (connReq) { 

      NSLog(@"Connection Sucessful"); 
      receivedData = [[NSMutableData alloc]init]; 
      [self facialimagelogcounturl]; 

     } 
     else 
     { 

      NSLog(@"failed"); 

     } 
     [connReq start]; 

     NSHTTPURLResponse *response = nil; 
     NSError *error = nil; 
     NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
     NSLog(@"Status code: %ld", (long)[response statusCode]); 
     NSLog(@"respdata%@",respData); 




    -(void)connection:(NSURLConnection *)connection didReceiveResponse: 
    (NSURLResponse *)response 
    { 
     [receivedData setLength:0]; 

    } 

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

     [receivedData appendData:data]; 


    } 

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 

     NSLog(@"ERROR"); 

    } 


    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 

     NSError *e; 
     NSString *JSON = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]; 
     _userlogcountlabel.text=JSON; 
     int json=[JSON intValue]; 
} 

我需要顯示在不同的標籤中的數據的每個從單個URL其工作罰款url.For單個取出單個值。我需要爲多個網址製作。

+1

環路爲你有網址的數量。爲每個URL在循環中獲取JSON響應。 –

+0

這裏你只顯示一個網址,你的四個網址在哪裏 –

+0

它在karthik上我有異步請求的答案。我會在幾分鐘後發佈 –

回答

2

這是我自己的問題的答案。

NSString *url1=[NSString stringWithFormat:@"http://jsonddata.in/UserImage.svc/GetNotificationCount?UserId=%@&CategoryId=1",requestString]; 
    NSURL *URL1 = [NSURL URLWithString:url1]; 
    NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:URL1] ; 

    [NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
     NSArray *results1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
     NSLog(@"facialimage log count %@",[results1.firstObject objectForKey:@"Notifications"]); 

    }]; 


    NSString *url2=[NSString stringWithFormat:@"http://jsonddata.in/UserImage.svc/GetNotificationCount?UserId=%@&CategoryId=2",requestString]; 
    NSURL *URL2 = [NSURL URLWithString:url2]; 
    NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc] initWithURL:URL2] ; 

    [NSURLConnection sendAsynchronousRequest:request2 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
     NSArray *results1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
     NSLog(@"clothing log count %@",[results1.firstObject objectForKey:@"Notifications"]); 

    }]; 

    NSString *url3=[NSString stringWithFormat:@"http://jsondata.in/UserImage.svc/GetNotificationCount?UserId=%@&CategoryId=3",requestString]; 
    NSURL *URL3 = [NSURL URLWithString:url3]; 
    NSMutableURLRequest *request3 = [[NSMutableURLRequest alloc] initWithURL:URL3] ; 

    [NSURLConnection sendAsynchronousRequest:request3 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
     NSArray *results1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 
     NSLog(@"object compare log count %@",[results1.firstObject objectForKey:@"Notifications"]); 

     } 

    }];