簡體   English   中英

如何在iPhone中連續調用nsurlconnection委托方法

[英]how call nsurlconnection delegate methods continuosly in iphone

您好在我的應用程序之一。 我必須連續多次向服務器(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調用服務器。

如果有人知道,請讓我知道。謝謝。

在.h文件中創建一個count變量int count。

 int count = 0 //in .m file

現在使用此方法:

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

Prince提出的解決方案並不通用,因為您將面臨為每個請求定義“ filterid”的問題。

而且您正在做的事情是一個不好的方法。 不要將Web請求與您的業務邏輯代碼混合使用。Web內容應由單獨的文件處理,以處理整個應用程序中的所有請求。 該類將執行委派。

對於委派,您需要執行以下操作。 在您的網絡類標頭(Networkclass.h)中添加協議

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

並在NetworkClass.m(實現文件)中執行以下操作

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

如果仍然遇到問題,可以參考以下URLConnection有什么問題?

或者您可以先閱讀任何異步下載教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM