在ios中异步调用Web服务

时间:2014-08-26 11:48:53

标签: ios web-services tableview

    NSData *responseData2= [NSURLConnection sendSynchronousRequest:request2 returningResponse:&urlResponse2 error:&error2];
    aparser =[[NSXMLParser alloc]initWithData:responseData2];

到目前为止,我正在调用一个服务并在数组中完全获取数据,然后在tableview中加载。但是当存在大量数据时需要很长时间才能运行,那么如何异步调用服务并加载tableview一次。

我想用Example来更好地理解。喜欢跟Xml响应,如果它有1000个学生要解析,所以我想获得第一个学生详细信息并将其加载到tableview和第二个学生详细信息并加载Tableview ....等等..

  <Class>
            <Student>
            <Name>Rama</Name>
            <Rollno>01</Rollno>
            </Student>
            <Student>
            <Name>Ravi</Name>
            <Rollno>02</Rollno>
            </Student>
         ......
         ......
  </Class>

提前致谢。

3 个答案:

答案 0 :(得分:0)

喜欢这样

NSOperationQueue *mainQueue = [[NSOperationQueue alloc] init];
[mainQueue setMaxConcurrentOperationCount:5];

NSURL *url = [NSURL URLWithString:@"http://192.168.0.63:7070/api/Misc/GetFuelTypes"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setHTTPMethod:@"GET"];
[request setAllHTTPHeaderFields:@{@"Accepts-Encoding": @"gzip", @"Accept": @"application/json"}];

[NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) {
    NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
    if (!error) {
        NSLog(@"Status Code: %li %@", (long)urlResponse.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode]);
        NSLog(@"Response Body: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
else {
    NSLog(@"An error occured, Status Code: %i", urlResponse.statusCode);
    NSLog(@"Description: %@", [error localizedDescription]);
    NSLog(@"Response Body: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
    }
}];

答案 1 :(得分:0)

在获取任何数据之前,请显示消息或加载指示符。这是一个不错的SO post如何做到这一点。

每当您获取数据时,请在您的tableview上调用reloadData,它将从委托方法numberOfRowsInSectioncellForRowAtIndexPath等重建自己。

答案 2 :(得分:-1)

-(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib.

    app=[UIApplication sharedApplication].delegate;

    xmldata=[[NSMutableData alloc]init];
    dict=[[NSDictionary alloc]init];
    mArray=[[NSMutableArray alloc]init];
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://indianbloodbank.com/api/donors/?bloodgroup=O%2B"]];
    NSURLConnection *Co=[NSURLConnection connectionWithRequest:request delegate:self];
    NSLog(@"Connection-%@",Co);
}

-(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response{return request;} -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"NETWORK ERROR"); }

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"%@",response); } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {[xmldata appendData:data]; NSLog(@"XMLData-%@",xmldata); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { dict=[XMLReader dictionaryForXMLData:xmldata error:nil]; NSLog(@"Dictionary-%@",dict); mArray=[dict retrieveForPath:@"response.donorslist.donors"]; NSLog(@"MutableArray-%@",mArray);

//DONORS ENTITY

    //Saving the data in to database
    for (int i=0; i<mArray.count; i++)
    {
        Database * don=[NSEntityDescription insertNewObjectForEntityForName:@"Donors" inManagedObjectContext:app.managedObjectContext];

        don.donid=[[mArray objectAtIndex:i]valueForKey:@"id"];
        don.gender=[[mArray objectAtIndex:i]valueForKey:@"gender"];
        don.name=[[mArray objectAtIndex:i]valueForKey:@"name"];
        don.location=[[mArray objectAtIndex:i]valueForKey:@"location"];
        don.phone=[[mArray objectAtIndex:i]valueForKey:@"phone"];

        [app saveContext];

        NSLog(@"Donors Entity Details-%@,%@,%@,%@,%@",[[mArray objectAtIndex:i]valueForKey:@"id"],[[mArray objectAtIndex:i]valueForKey:@"gender"],[[mArray objectAtIndex:i]valueForKey:@"name"],[[mArray objectAtIndex:i]valueForKey:@"location"],[[mArray objectAtIndex:i]valueForKey:@"phone"]);

        [tbl reloadData];
    }
}
相关问题