0

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

@"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

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

but i would like to call the server for each filterid.

if anyone know please let me know.thanks in advance.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
Naresh
  • 19
  • 2

2 Answers2

0

Create one count variable int count in .h file.

 int count = 0 //in .m file

Now use this method:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  //one request finished
  count ++;
  //request another using count
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • hi prince i need some more help from you.Actually i will get the response from the server with different filterid. i dont know the count of filters. based on filterid's count i have to create many array's dynamically in runtime based on number of filterid's for storing the information about each filter (information may be images or metadata what ever it may be i have to store that in array). may i know how can we create array's dynamically .thanks in advance. – Naresh Aug 21 '12 at 07:07
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.

Community
  • 1
  • 1
yunas
  • 4,143
  • 1
  • 32
  • 38