我在 appdelegate.m 中对应用程序启动和退出发出 3 个 NSURLConnection 请求,我正在使用异步调用,返回的响应是字典数组,所有响应中的数组都具有相同的名称,所以我无法检查键。
所以我尝试在 appdelgate.h 中声明 NSURLConnection 变量,然后在 connectionDidFinishloading 中将每个声明的连接对象与连接参数进行比较
//Appdelegate.h
NSURLConnection *conn1;
NSURLConnection *conn2;
NSURLConnection *conn3;
然后在//Appdelgate.m
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xyz/login"]];
request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"GET"];
conn1 = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[conn1 start];
然后
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSError *e = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData: m_responseData options: 0 error: &e];
//Extracy spcific keys and add to respective arrays
if(connection == conn1)
{
//call 2 nd request
}
if(connection == conn2)
{
//call 3 rd request
}
}
我检查了连接对象的信息,它总是显示这个
<NSURLConnection: 0x14d09c20> { request: <NSMutableURLRequest: 0x14d0ac70> { URL: http://xyz/login } }
即使在调用第二个请求后,它仍然显示第一个请求。
所以比较失败..
那么对此有什么更好的方法,还是我遗漏了什么。
最佳答案
就像使用一样
NSURLConnection *itemIdConnection,*contactNameConnection;
NSMutableData *receivedData, *locationData;
你的委托(delegate)方法是
#pragma NSUrlConnectionDelegate Methods
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
if(connection == itemIdConnection)
{
if (receivedData == NULL)
{
receivedData = [[NSMutableData alloc] init];
}
[receivedData setLength:0];
}
if(connection == contactNameConnection)
{
if (locationData == NULL)
{
locationData = [[NSMutableData alloc] init];
}
[locationData setLength:0];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if (connection==itemIdConnection) {
[receivedData appendData:data];
}
if(connection == contactNameConnection)
{
[locationData appendData:data];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
UIAlertView *customAlert = [[UIAlertView alloc]initWithTitle:@"No NetWork" message:@"Interet Connection is Lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[customAlert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection==itemIdConnection) {
// NSError *e = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData options: kNilOptions error:nil];
NSString *tmp=[[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", tmp);
NSLog(@" parsing JSON: %@", jsonDict);
}
if (connection==contactNameConnection) {
NSError *e = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:locationData options: NSJSONReadingMutableContainers error: &e];
NSLog(@" parsing JSON: %@", jsonDict);
}
}
关于ios - 如何管理多个异步 NSURLConnection 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075772/