0

嗨,在我的一个应用程序中。我必须连续多次向服务器(json服务器)发送请求。我的网址将如下所述

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

实际上我有很多过滤器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");
        }

    }

实际上,在 for 循环中满足每个条件后,我必须连接服务器以使用 nsurlconnection 委托方法从服务器获取数据。但是在这里,在完全执行 for 循环之后,只有 nsurlconnection 委托方法使用从 appdelegate.listOfFiltersArray 数组获取的最后一个 filterid 执行。

但我想为每个 filterid 调用服务器。

如果有人知道,请让我知道。提前谢谢。

4

2 回答 2

0

在 .h 文件中创建一个计数变量 int count。

 int count = 0 //in .m file

现在使用这个方法:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  //one request finished
  count ++;
  //request another using count
}
于 2012-08-21T06:46:33.797 回答
0

The solution that Prince proposed is not general as you will face the problem of defining the "filterid" for each request.

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. And that class will implement delegation.

For delegation you need to do the following. In your Network class header (Networkclass.h) add the protocol

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

and in NetworkClass.m (implementation fie) do the following

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

or you may read any asynchronous downloading tutorial first.

于 2012-08-21T10:11:38.380 回答