简体   繁体   English

如何在iPhone中连续调用nsurlconnection委托方法

[英]how call nsurlconnection delegate methods continuosly in iphone

hi in one of my application. 您好在我的应用程序之一。 i have to send a request to the server (json server) many times continously.my url will be like this as mention below 我必须连续多次向服务器(json服务器)发送请求。我的网址如下所示

@"http://185.185.116.51/servername/serverjspfilesname.jsp?filterID=21&ticket=65675656565656567" @“ http://185.185.116.51/servername/serverjspfilesname.jsp?filterID=21&ticket=65675656565656567”

actually i have many filter id's (filter id you can find at top).in order to chnage the filterid continously i used for loop like this as mention below 实际上我有很多过滤器ID(您可以在顶部找到过滤器ID)。为了连续更改过滤器ID,我使用了如下所述的循环

for(int i=0;i<[appdelegate.listOfFiltersArray count];i++)
    {

        filtersDataModelObject=[[ListOfFiltersDataModel alloc]init];

        filtersDataModelObject=[appdelegate.listOfFiltersArray objectAtIndex:i];

       homescreenstring=[NSString stringWithFormat:@"http://%@/servername/serverjspfilesname.jsp?filterID=%@&ticket=%@",Ip,filtersDataModelObject.filterID,[storeData stringForKey:@"securityTicket"]];


        NSLog(@"url is %@",homescreenstring);

        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:homescreenstring]];

        connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

        if(connection)
        {

            homeScreenResponseData=[[NSMutableData alloc]init];

        }
        else
        {

            NSLog(@"connection failed");
        }

    }

actually after each condition is satisfied in for loop i have to connect with the server for getting the data from the server using nsurlconnection delegate methods. 实际上,在for循环中满足每个条件之后,我必须与服务器连接以使用nsurlconnection委托方法从服务器获取数据。 but here after complete execution of for loop only nsurlconnection delegate methods are executing with last filterid which is getting from the appdelegate.listOfFiltersArray array. 但是在完成for循环之后,只有nsurlconnection委托方法才使用从appdelegate.listOfFiltersArray数组获取的最后一个filterid执行。

but i would like to call the server for each filterid. 但我想为每个filterid调用服务器。

if anyone know please let me know.thanks in advance. 如果有人知道,请让我知道。谢谢。

Create one count variable int count in .h file. 在.h文件中创建一个count变量int count。

 int count = 0 //in .m file

Now use this method: 现在使用此方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  //one request finished
  count ++;
  //request another using count
}

The solution that Prince proposed is not general as you will face the problem of defining the "filterid" for each request. Prince提出的解决方案并不通用,因为您将面临为每个请求定义“ filterid”的问题。

And what you are doing is a bad approach. 而且您正在做的事情是一个不好的方法。 Dont mix the web request with your business logic code.The web stuff should be handled by a separate file handling all the requests throughout the app. 不要将Web请求与您的业务逻辑代码混合使用。Web内容应由单独的文件处理,以处理整个应用程序中的所有请求。 And that class will implement delegation. 该类将执行委派。

For delegation you need to do the following. 对于委派,您需要执行以下操作。 In your Network class header (Networkclass.h) add the protocol 在您的网络类标头(Networkclass.h)中添加协议

@protocol NetworkControllerDelegate <NSObject>
-(void) UrlResponseRecieved :(NSData *)responseData;
-(void) UrlResponseFailed:(NSError *)error;
@end

and in NetworkClass.m (implementation fie) do the following 并在NetworkClass.m(实现文件)中执行以下操作

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    if (receivedData != nil) 
    {
       if([delegate respondsToSelector:@selector(UrlResponseRecieved:)])
        [self.delegate UrlResponseRecieved:receivedData];
        [receivedData release];
        receivedData = nil;
    }
    [connection release];
}

if you still get stuck you may refer to the following What's wrong on following URLConnection? 如果仍然遇到问题,可以参考以下URLConnection有什么问题?

or you may read any asynchronous downloading tutorial first. 或者您可以先阅读任何异步下载教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM