How can I send multiple url request from a NSURLConnection delegate?

匆匆过客 提交于 2019-12-09 06:43:15

问题


This is the logical flow for my application:

  1. At first, when the view controller has finished loading, then a NSURLConnection request can start its execution

  2. The response consists in xml data

  3. After parsing that xml I need to send another NSURLConnection request.

  4. After sending the second request, if the response is ok, I receive other xml data

  5. After parsing the second xml, I have to check some issues between first and second xml data.

So, is it possible to send multiple request? How? I do not need code, you could just explain it.

Thank you in advance.


回答1:


- (void)viedDidLoad{

    [super viewDidLoad];
    [self firstRequestMethod];

}

- (void)firstRequestMethod{

    NSString *myFirstRequestURL = @"<URL>";
    NSURL *webURL = [NSURL URLWithString:myFirstRequestURL];        

    NSURLRequest *request = [NSURLRequest requestWithURL:webURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSError *error;
    NSURLResponse *response;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if(returnData)
    {
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
        //Parse your response here.
        //Is desired response obtain call the second Request, as described above
        if (TRUE) {  //on success
            [self secondRequestMethod];
        }

    }



}

- (void)secondRequestMethod{

    NSString *mySecondRequestURL = @"<URL>";
    NSURL *webURL = [NSURL URLWithString:mySecondRequestURL];        

    NSURLRequest *request = [NSURLRequest requestWithURL:webURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSError *error;
    NSURLResponse *response;
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if(returnData)
    {
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
        //Parse your response here.
        //Is desired response obtain call the second Request, as described above
        if (TRUE) {  //on success
            //subsequent calls to other url, same as above
        }

    }



}

Hope this will help you understand better....




回答2:


I do this with the NSURLConnection Making them properties, then checking which one it is:

 @property (nonatomic,retain) NSURLConnection *myConnection;
@property (nonatomic,retain) NSURLConnection *mySecondConnection;

then in the delegate:

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

     if (connection == myConnection){
       //do something
    }

    if (connection == mySecondConnection){
       // do something else
        }

}

You can pass your NSURLRequest to the connection:

self.myConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];



回答3:


There is a third party library available which is a wrapper on CFNetwork is ASIHTTPREQUEST

This Library should do work for you. so that you don't have to write the code from scratch. other alternative is create one class which will be responsible for creating NSURLConnection then sending the request and finally notify to view controller using delegate or notification one data is received .



来源:https://stackoverflow.com/questions/8982030/how-can-i-send-multiple-url-request-from-a-nsurlconnection-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!