如何从 NSURLConnection 委托发送多个 url 请求? [英] How can I send multiple url request from a NSURLConnection delegate?

查看:15
本文介绍了如何从 NSURLConnection 委托发送多个 url 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的应用程序的逻辑流程:

This is the logical flow for my application:

  1. 首先,当视图控制器完成加载后,一个NSURLConnection请求就可以开始执行了

响应包含在 xml 数据中

The response consists in xml data

解析该 xml 后,我需要发送另一个 NSURLConnection 请求.

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

发送第二个请求后,如果响应ok,则收到其他xml数据

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

解析第二个xml后,我必须检查第一个和第二个xml数据之间的一些问题.

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.

提前谢谢你.

推荐答案

- (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....

这篇关于如何从 NSURLConnection 委托发送多个 url 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆