2011-08-12 198 views
0

我想異步獲取JSON數據。數據的設置方式是一個請求只能帶來8條記錄。我需要重複發送請求,直到響應變爲空或返回少於8條記錄。異步獲取JSON數據

目前,我在myviewcontroller.m類這些方法:

(void)myCallback:(id)sender { 

    MyDataRequest *objMyDataRequest = [[[MyDataRequest alloc] init] autorelease]; 
    objMyDataRequest.myRequiredVariableToGetAuthTokenDataResponse = classOfMyCallBack.someVariable; 

    // Initiate getAuthToken request 
    [objWishListRequest initiateGetAuthTokenRequest:self requestSelector:@selector(getAuthTokenDataResponse:)]; 
} 

現在這裏是getAuthTokenDataResponse的定義:

(void) getAuthTokenDataResponse:(NSData *)data { 

    NSString *stringResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
    SBJsonParser *parser=[[SBJsonParser alloc]init]; 
    NSDictionary *objDictionaryForStringResponse = [parser objectWithString:stringResponse]; 
    [stringResponse release]; 
    [parser release]; 
    MyListRequest *objMyListRequest = [[[MyListRequest alloc] init] autorelease]; 
    objMyListRequest.myRequiredValueToGetMyDataResponse = [objDictionaryForStringResponse objectForKey:@"Data"]; 

    // Initiate GetMyDataResponse request 
    [objMyListRequest initiateGetMyDataRequest:self requestSelector:@selector(getMyDataResponse:)]; 

} 


(void) getMyDataResponse:(NSData *)data { 

    NSString *stringResponse = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
    SBJsonParser *parser=[[SBJsonParser alloc]init]; 
    NSDictionary *objGetMyDataRootDictionary = [parser objectWithString:stringResponse]; 
    [stringResponse release]; 
    [parser release]; 
    NSDictionary *dataElements=[objGetMyDataRootDictionary objectForKey:@"Data"]; 
    Wish *objMyData; 

    for (NSDictionary* objGetMyDataRootDictionary in dataElements) { 
     objMyData = [[Wish alloc]init]; 
     //add different elements from dataElements into member variables of object objWish 
     [self.myDataArray addObject:objMyData]; 
     [objMyData release]; 
    } 
    [self.myDataTableView reloadData]; 
} 

這種方法在於MyDataRequest類:

(void)initiateGetMyDataRequest:(id)requestDelegate requestSelector:(SEL)requestSelector{ 

    // Set the delegate and selector 
    self.delegate = requestDelegate; 
    self.callback = requestSelector; 

    NSString* unescapedUrlString = [NSString stringWithFormat:@"http://test.mytesturl.com/core.svc/alldata/My/get/All/?token=%@&search=&page=1",myRequiredtokenparameter]; 
    [self request:url]; 
} 

我需要發送多個請求到相同的網址(使用不同的參數值,即頁碼的值)來獲取結果。鑑於上述情況,我該如何實現?這些調用必須是異步的。

我應該如何在所有這些調用之間進行實際流動?我怎樣才能異步獲取「所有頁面」的數據?

回答

0

我想你正在尋找一個操作隊列。我在我的應用程序中使用ASIHTTPRequests,它們工作。

如果你想使用這個庫,這裏的鏈接如何使用它:Show UIActivityIndicatorView when loading NSString from Web

+0

抱歉剛纔注意到您發送答覆我的問題。我想,當答覆將發佈在這裏時,我會收到一封電子郵件。無論如何,我已經找出了大部分的東西。留下一些小問題,例如如何處理UITableView上的滾動。詢問它[這裏](http://stackoverflow.com/questions/7050659/need-sample-code-to-implement-paging-based-uiscrollview/7050791#7050791)如果你能幫助我。 – salman