2012-03-09 33 views
0

嗨全部代碼如何獲得數組中的總響應實際上我已經實現了代碼,如下所示我的問題是當我在下一個類中調用方法時我得到的數組響應爲空,所以plz幫助我在iphone中避免這個問題。如何將響應數據導入數組?

我的代碼是:

- (無效)countrySelection {

NSString *jobSearchUrlString = [NSString stringWithFormat:@"http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Countries"]; 
    //NSLog(@"url for new articles is = %@",jobSearchUrlString); 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]]; 

    NSURLConnection *theconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    if (theconnection) { 
     RoutData = [[NSMutableData alloc] init]; 
    } 

} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

    [RoutData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [RoutData appendData:data]; 

      NSString *thexml = [[NSString alloc] initWithData:RoutData encoding:NSUTF8StringEncoding]; 

    NSDictionary *dictXML= [XMLReader dictionaryForXMLString:thexml error:nil]; 

    NSMutableArray *arrStation = [[dictXML objectForKey:@"feed"] objectForKey:@"entry"] ;//this would return the array of station dictionaries 

    for (int i=0; i<[arrStation count]; i++) { 
     NSLog(@"---->> String Type Country names <<---: %@",[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:CountryName"] objectForKey:@"text"]); 
     [materialarray addObject:[[[[[arrStation objectAtIndex:i] objectForKey:@"content"] objectForKey:@"m:properties"] objectForKey:@"d:CountryName"] objectForKey:@"text"]]; 

     NSLog(@"--->>** Array Type Country names **<<<---%@",materialarray); 


    } 



} 

我的回答是:

2012-03-09 12:49:40.856 TABLESAMPLE [4862:F803] - --- >>字符串類型國家名稱< < ---: 法國 2012-03-09 12:49:40.867 TableSample [4862:f803] --- >> **陣列類型國家名稱< < < ---(null) 2012-03-09 12:49:40.868 TableSample [4862:f803] ---- >>字符串類型國家名稱< < ---: MEA HQ 2012-03-09 12:49:40.869 TableSample [4862:f803] --- >>陣列類型國家名稱< < < ---(null) 2012-03-09 12:49:40.870 TableSample [4862:f803] - - >>字符串類型國名< < ---: 以色列 2012-03-09 12:49:40.871 TABLESAMPLE [4862:F803] --- >>陣列類型國名< < < --- (null) 2012-03-09 12:49:40.872 TableSample [4862:f803] ---- >>字符串類型國家名稱< < ---: 土耳其 2012-03-09 12:49:40.879 TableSample [4862: f803] --- >> Array Type Country < < < ---(null) 2012-03-09 12:49:40.879 TableSample [4862:f803] ---- >> >> String Type Country names < < ---: UAE 2012-03-09 12:49:40.880 TABLESAMPLE [4862:F803] --- >>陣列類型國名< < < ---(空) 2012-03-09 12:49:40.881 TableSample [4862:f803] ---- >>字符串類型國名< < ---: SA 2012-03-09 12:49:40.881 TABLESAMPLE [4862:F803] --- >>陣列類型國名< < < ---(空) 2012-03-09 12:49:40.882 TableSample [4862:f803] ---- >>字符串類型國家名稱< < ---: RSA 2012-03-09 12:49:41.020 TableSample [4862:f803 ] --- >> Array Type Country < < < ---(null) 2012-03-09 12:49:41.021 TableSample [4862:f803] ---- >> >>字符串類型國名< < ---: WE HQ 2012-03-09 12:49:41.022 TABLESAMPLE [4862:F803] --- >>陣列類型國名** < < < ---(空

+0

我知道答案,但我不喜歡給你一個答案! :) – 2012-03-09 07:31:03

回答

2

connectionDidFinishLoading委託方法你會得到響應字符串,你需要在該響應字符串上調用JSONValue它會給你一個數組中的結果。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    [connection release]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"responseString %@",responseString); 
    self.responseData = nil; 
    NSArray *responseArray = [responseString JSONValue]; 
    [responseString release]; 
} 
+0

對不起朋友它不適用於我的應用程序becoz基本上它是xml解析。爲我提供另一個答案 – laxmanperamalla 2012-03-09 07:46:50

+0

您正在實施JSON解析器委託方法。如果您使用XML解析,則需要實施NSXMLParser委託方法。這裏是它的鏈接:https://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html – Dee 2012-03-09 07:50:12

1

請確保已分配materialarray

我認爲問題出在哪裏。

希望這會有所幫助。