我需要从四个网址中获取单个JSON数据值。为了从单个URL获取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获取单个值,它的工作正常。我需要为多个网址制作它。
答案 0 :(得分:0)
以下是我自己的问题的答案。
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"]);
}
}];