2013-05-27 57 views
0

我當前的問題是我已經成功地從使用SBJSON的Web服務中獲取數據,並且將數據解析爲包含多個變量的類。從主視圖控制器訪問已解析的JSON數據iOS

現在,我的主視圖控制器是應該訪問是從網絡服務,但我每次訪問的變量時間拉取數據類中的變量,都爲空或0

我想這個問題是因爲當應用程序已經啓動到主視圖控制器時,從Web服務器獲取數據的類尚未加載。

我該如何解決這個問題?

這裏是主視圖控制器的代碼。

- (void)viewDidLoad 
{ 

    _mainParser = [[MonroeParser alloc] init]; 
    [_mainParser go]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UINavigationBar *navBar = [[self navigationController] navigationBar]; 
    navBar.tintColor = [UIColor redColor]; 
    UIImageView* img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Power_One_RedLogo.png"]]; 
    self.navigationItem.titleView = img; 

    UIBarButtonItem *iconButton = [[UIBarButtonItem alloc] initWithTitle:@"View Site" style:UIBarButtonItemStylePlain target:self action:@selector(pushToPowerOneWebsite:)]; 
    self.navigationItem.leftBarButtonItem = iconButton; 


    _DayString.text = @"Thursday"; 
    _DayInteger.text = @"24"; 
    _MonthString.text = @"May"; 
    _SystemStatusIcon.image = [UIImage imageNamed:@"Power_One_RedLogo.png"] ; 
    _SystemStatus.text = @"Normal"; 

    [_StatusButton addSubview:_DayInteger]; 



    NSLog(@"%@", _mainParser.solarStatus); 
    NSLog(@"%@", _mainParser.fields); 
    NSLog(@"%@", _mainParser.type); 
    NSLog(@"%@", _mainParser.field); 
    NSLog(@"%@", _mainParser.label); 
    NSLog(@"%ld", (long)_mainParser.entityID); 
    NSLog(@"%@", _mainParser.entityName); 
    NSLog(@"%@", _mainParser.timeZone); 
    NSLog(@"%@", _mainParser.units); 
    NSLog(@"%@", _mainParser.parameters); 
    NSLog(@"%ld", (long)_mainParser.param_Value); 
    NSLog(@"%@", _mainParser.param_Name); 
    NSLog(@"%ld", (long)_mainParser.start); 
    NSLog(@"%@", _mainParser.startLabel); 
    NSLog(@"%ld", (long)_mainParser.end); 
    NSLog(@"%@", _mainParser.endLabel); 
    NSLog(@"%ld", (long)_mainParser.value); 
} 

我的解析器類代碼:

- (void) go 
{ 
    adapter = [[SBJsonStreamParserAdapter alloc] init]; 
    adapter.delegate = self; 

    parser = [[SBJsonStreamParser alloc] init]; 
    parser.delegate = adapter; 

    parser.supportMultipleDocuments = YES; 

    NSString *url = myurl; 

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
} 

#pragma mark SBJsonStreamParserAdapterDelegate methods 

- (void)parser:(SBJsonStreamParser *)parser foundArray:(NSArray *)array 
{ 
    [NSException raise:@"unexpevted" format:@"Should not get here"]; 
} 


- (void)parser:(SBJsonStreamParser *)parser foundObject:(NSDictionary *)dict 
{ 
    solarStatus = [dict objectForKey:@"status"]; 
    fields = [dict objectForKey:@"fields"]; 
    type = [[dict objectForKey:@"fields"] valueForKey:@"type"]; 
    field = [[dict objectForKey:@"fields"] valueForKey:@"field"]; 
    label = [[dict objectForKey:@"fields"] valueForKey:@"label"]; 
    entityID = [[dict objectForKey:@"fields"] valueForKey:@"entityID"]; 
    entityName = [[dict objectForKey:@"fields"] valueForKey:@"entityName"]; 
    timeZone = [[dict objectForKey:@"fields"] valueForKey:@"entityName"]; 
    units = [[dict objectForKey:@"fields"] valueForKey:@"units"]; 
    parameters = [[dict objectForKey:@"fields"] valueForKey:@"parameters"]; 
    param_Value = [[[dict objectForKey:@"fields"] valueForKey:@"parameters"]valueForKeyPath:@"value"]; 
    param_Name = [[[dict objectForKey:@"fields"] valueForKey:@"parameters"]valueForKeyPath:@"name"]; 
    start = [[dict objectForKey:@"fields"] valueForKey:@"start"]; 
    startLabel = [[dict objectForKey:@"fields"] valueForKey:@"startLabel"]; 
    end = [[dict objectForKey:@"fields"] valueForKey:@"end"]; 
    endLabel = [[dict objectForKey:@"fields"] valueForKey:@"endLabel"]; 
    value = [[dict objectForKey:@"fields"] valueForKey:@"value"]; 



    NSLog(@"%@", solarStatus); 
    NSLog(@"%@", fields); 
    NSLog(@"%@", type); 
    NSLog(@"%@", field); 
    NSLog(@"%@", label); 
    NSLog(@"%ld", (long)entityID); 
    NSLog(@"%@", entityName); 
    NSLog(@"%@", timeZone); 
    NSLog(@"%@", units); 
    NSLog(@"%@", parameters); 
    NSLog(@"%ld", (long)param_Value); 
    NSLog(@"%@", param_Name); 
    NSLog(@"%ld", (long)start); 
    NSLog(@"%@", startLabel); 
    NSLog(@"%ld", (long)end); 
    NSLog(@"%@", endLabel); 
    NSLog(@"%ld", (long)value); 

} 
#pragma mark NSURLConnectionDelegate methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Connection didReceiveResponse: %@ - %@", response, [response MIMEType]); 
} 



- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    NSLog(@"Connection didReceiveAuthenticationChallenge: %@", challenge); 

    NSURLCredential *credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]; 

    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
} 



- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSLog(@"Connection didReceiveData of length: %u", data.length); 

    SBJsonStreamParserStatus status = [parser parse:data]; 

    if (status == SBJsonStreamParserError) 
    { 
     solarStatus = [NSString stringWithFormat:@"The parser encountered an error: %@", parser.error]; 
     NSLog(@"Parser error: %@", parser.error); 

    } else if (status == SBJsonStreamParserWaitingForData) { 
     NSLog(@"Parser waiting for more data"); 
    } 
} 



- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 



- (void)connectionDidFinishLoading:(NSURLConnection*)connection 
{ 
    NSLog(@"Connection finished"); 
} 

類解析器的輸出輸出與實際數據的正確字符串。但問題是,當在主視圖控制器中輸出相同的數據時,所有內容都返回NULL或0.

就像我說的,我確定問題在於,在類解析器甚至可以檢索獲取的JSON對象的DATA。

任何幫助將是偉大的!

此外,我做了一些研究,似乎我可能需要實現一個函數來使用異步調用Web服務我從中獲取數據,但我很新,這樣的實現。

輸出

2013-05-27 16:18:26.580 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.585 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.585 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.586 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.586 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.586 PowerOneAuroraApp[14230:c07] 0 
2013-05-27 16:18:26.586 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.587 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.587 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.587 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.588 PowerOneAuroraApp[14230:c07] 0 
2013-05-27 16:18:26.588 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.588 PowerOneAuroraApp[14230:c07] 0 
2013-05-27 16:18:26.603 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.603 PowerOneAuroraApp[14230:c07] 0 
2013-05-27 16:18:26.603 PowerOneAuroraApp[14230:c07] (null) 
2013-05-27 16:18:26.604 PowerOneAuroraApp[14230:c07] 0 
2013-05-27 16:18:27.092 PowerOneAuroraApp[14230:c07] Connection didReceiveResponse: <NSHTTPURLResponse: 0x7539100> - application/json 
2013-05-27 16:18:27.092 PowerOneAuroraApp[14230:c07] Connection didReceiveData of length: 2154 
2013-05-27 16:18:27.094 PowerOneAuroraApp[14230:c07] SUCCESS 
2013-05-27 16:18:27.095 PowerOneAuroraApp[14230:c07] (
     { 
     end = 1369696440000; 
     endLabel = "Mon May 27, 2013 5:14:00 PM MDT"; 
     entityId = 1167815; 
     entityName = "Morone Residence"; 
     field = GenerationPower; 
     label = now; 
     parameters =   (
     ); 
     start = 1369696440000; 
     startLabel = "Mon May 27, 2013 5:14:00 PM MDT"; 
     timeZone = "US/Mountain"; 
     type = instant; 
     units = kilowatts; 
     value = "0.229000000283"; 
    }, 
     { 
     end = 1369696707064; 
     endLabel = 20130527171827; 
     entityId = 1167815; 
     entityName = "Morone Residence"; 
     field = GenerationEnergy; 
     label = today; 
     parameters =   (
         { 
       name = "DataItem.now.maxCacheAge"; 
       value = 60; 
      } 
     ); 
     sampleEnd = 1369692840000; 
     sampleEndLabel = 20130527161400; 
     sampleStart = 1369634340000; 
     sampleStartLabel = 20130526235900; 
     start = 1369634400000; 
     startLabel = 20130527000000; 
     timeZone = "US/Mountain"; 
     type = window; 
     units = "kilowatt-hours"; 
     value = "7.95501708984"; 
    }, 
     { 
     end = 1369696707064; 
     endLabel = 20130527171827; 
     entityId = 1167815; 
     entityName = "Morone Residence"; 
     field = GenerationEnergy; 
     label = week; 
     parameters =   (
         { 
       name = "DataItem.now.maxCacheAge"; 
       value = 60; 
      } 
     ); 
     sampleEnd = 1369692840000; 
     sampleEndLabel = 20130527161400; 
     sampleStart = 1369547940000; 
     sampleStartLabel = 20130525235900; 
     start = 1369548000000; 
     startLabel = 20130526000000; 
     timeZone = "US/Mountain"; 
     type = window; 
     units = "kilowatt-hours"; 
     value = "16.60800170898"; 
    }, 
     { 
     end = 1369696707064; 
     endLabel = 20130527171827; 
     entityId = 1167815; 
     entityName = "Morone Residence"; 
     field = GenerationEnergy; 
     label = month; 
     parameters =   (
         { 
       name = "DataItem.now.maxCacheAge"; 
       value = 60; 
      } 
     ); 
     sampleEnd = 1369692840000; 
     sampleEndLabel = 20130527161400; 
     sampleStart = 1367387940000; 
     sampleStartLabel = 20130430235900; 
     start = 1367388000000; 
     startLabel = 20130501000000; 
     timeZone = "US/Mountain"; 
     type = window; 
     units = "kilowatt-hours"; 
     value = "269.87199401855"; 
    }, 
     { 
     end = 1369692840000; 
     endLabel = "Mon May 27, 2013 4:14:00 PM MDT"; 
     entityId = 1167815; 
     entityName = "Morone Residence"; 
     field = GenerationEnergy; 
     label = lifetime; 
     parameters =   (
     ); 
     start = 1369692840000; 
     startLabel = "Mon May 27, 2013 4:14:00 PM MDT"; 
     timeZone = "US/Mountain"; 
     type = instant; 
     units = "kilowatt-hours"; 
     value = "1609.48400878906"; 
    } 
) 
2013-05-27 16:18:27.097 PowerOneAuroraApp[14230:c07] (
    instant, 
    window, 
    window, 
    window, 
    instant 
) 
2013-05-27 16:18:27.098 PowerOneAuroraApp[14230:c07] (
    GenerationPower, 
    GenerationEnergy, 
    GenerationEnergy, 
    GenerationEnergy, 
    GenerationEnergy 
) 
2013-05-27 16:18:27.098 PowerOneAuroraApp[14230:c07] (
    now, 
    today, 
    week, 
    month, 
    lifetime 
) 
2013-05-27 16:18:27.098 PowerOneAuroraApp[14230:c07] 124196320 
2013-05-27 16:18:27.099 PowerOneAuroraApp[14230:c07] (
    "Morone Residence", 
    "Morone Residence", 
    "Morone Residence", 
    "Morone Residence", 
    "Morone Residence" 
) 
2013-05-27 16:18:27.099 PowerOneAuroraApp[14230:c07] (
    "Morone Residence", 
    "Morone Residence", 
    "Morone Residence", 
    "Morone Residence", 
    "Morone Residence" 
) 
2013-05-27 16:18:27.099 PowerOneAuroraApp[14230:c07] (
    kilowatts, 
    "kilowatt-hours", 
    "kilowatt-hours", 
    "kilowatt-hours", 
    "kilowatt-hours" 
) 
2013-05-27 16:18:27.104 PowerOneAuroraApp[14230:c07] (
     (
    ), 
     (
       { 
      name = "DataItem.now.maxCacheAge"; 
      value = 60; 
     } 
    ), 
     (
       { 
      name = "DataItem.now.maxCacheAge"; 
      value = 60; 
     } 
    ), 
     (
       { 
      name = "DataItem.now.maxCacheAge"; 
      value = 60; 
     } 
    ), 
     (
    ) 
) 
2013-05-27 16:18:27.105 PowerOneAuroraApp[14230:c07] 124196576 
2013-05-27 16:18:27.105 PowerOneAuroraApp[14230:c07] (
     (
    ), 
     (
     "DataItem.now.maxCacheAge" 
    ), 
     (
     "DataItem.now.maxCacheAge" 
    ), 
     (
     "DataItem.now.maxCacheAge" 
    ), 
     (
    ) 
) 
2013-05-27 16:18:27.106 PowerOneAuroraApp[14230:c07] 124196720 
2013-05-27 16:18:27.106 PowerOneAuroraApp[14230:c07] (
    "Mon May 27, 2013 5:14:00 PM MDT", 
    20130527000000, 
    20130526000000, 
    20130501000000, 
    "Mon May 27, 2013 4:14:00 PM MDT" 
) 
2013-05-27 16:18:27.106 PowerOneAuroraApp[14230:c07] 124196784 
2013-05-27 16:18:27.106 PowerOneAuroraApp[14230:c07] (
    "Mon May 27, 2013 5:14:00 PM MDT", 
    20130527171827, 
    20130527171827, 
    20130527171827, 
    "Mon May 27, 2013 4:14:00 PM MDT" 
) 
2013-05-27 16:18:27.107 PowerOneAuroraApp[14230:c07] 124196848 
2013-05-27 16:18:27.107 PowerOneAuroraApp[14230:c07] Parser waiting for more data 
2013-05-27 16:18:27.107 PowerOneAuroraApp[14230:c07] Connection finished 

回答

1

你不應該這樣做。在你想讀取它之前,你將永遠無法從URL中獲取信息,除非你讓它同步,這意味着你的UI將被卡住在viewdidload中,直到它完成,這是一個可怕的做法。

我對SBJSON並不熟悉,但它很可能有一個委託方法調用或完成處理程序塊,您應該在那裏處理檢索到的信息。

編輯:其實我的不好,我沒有意識到你正在使用NSURLConnection來獲取json數據。另外,我可以在你的代碼中看到其他幾個奇怪的東西。

您只需檢查蘋果文檔here並在您的主控制器中執行:connectionDidFinishLoading

獲得JSON數據的響應後,您應該將其提供給解析器,然後您可以使用該信息。

在此代碼中,我無法看到您的連接委託,也無法將這些數據提供給解析器。

this

SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; 
NSError *error = nil; 
NSArray *jsonObjects = [jsonParser objectWithString:jsonString error:&error]; 
[jsonParser release], jsonParser = nil; 

但在它出現,所以我假定你正在做正確的日誌。

檢查日誌,

連接didReceiveResponse:

後會出現你所要求的數據,出現此調用之前那麼那裏有什麼。移動您正在使用的任何代碼來解析信息到該方法。

+0

這就是我想我必須做的,但我對JSON對象非常陌生,並且處理這些回調和完成處理程序塊。如果有人有如何做到這一點的任何例子....我將不勝感激!我引用了SBJSON API文檔,但就像我說的,我很新,並且讓我走上正軌的榜樣會非常有幫助。希望別人有同樣的問題,並解決它,所以我不會需要幾個小時,如果沒有幾天來解決這個問題。 – jsetting32

+0

檢查編輯,因爲這不是你的完整代碼即時通訊不知道你是如何做的事情,但我認爲修復很簡單。 (把你的代碼從那個地方移到didfinishloading委託調用) – Pochi

相關問題