2014-09-03 28 views
-2

我嘗試從json下載數據從web服務器到我的ios應用程序。IOS下載數據與JSON的iPhone應用程序

That's的JSON輸出

{ 
    "responseHeader":{ 
     "status":0, 
     "QTime":37, 
     "params":{ 
      "wt":"json", 
      "q":"title:ios" 
     } 
    }, 
    "response":{ 
     "numFound":348, 
     "start":0, 
     "docs":[ 
      { 
       "edition":"2. ed.", 
       "illustrated":"Not Illustrated", 
       "id":"BSZ117259543", 
       "author":"Raw, Charles", 
       "title":"IOS /", 
       "spelling":"Raw, Charles (DE-576)180904566 Do you sincerely want to be rich? span. IOS/Charles Raw; Bruce Page; Godfrey Hodgson 2. ed. São Paulo : Ed. Expressão e Cultura, 1972 XV, 496 S. Page, Bruce (DE-576)162468539 aut Hodgson, Godfrey (DE-576)161444822 aut", 
       "last_indexed":"2012-09-02T01:11:38Z", 
       "recordtype":"marc", 
       "title_auth":"IOS /", 
       "title_sort":"ios", 
       "title_short":"IOS /", 
       "fullrecord":"00985nam a22003372" 
      } 
     ] 
    } 
} 

我的項目的代碼:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

// 1 Schritt 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 
//code executed in the background 
// 2 
//NSData* kivaData = [NSData dataWithContentsOfURL:[NSURLURLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]; 

    NSData* kivaData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://bib.flux-services.net/solr/biblio/select?q=title:ios&wt=json"]]; 

    //3 

    NSDictionary* json = nil; 
    if (kivaData) { 
     json = [NSJSONSerialization JSONObjectWithData:kivaData options:kNilOptions error:nil]; 
    } 

    //4 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self updateUIWithDictionary:json]; 
    }); 

}); 
} 



- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

-(void)updateUIWithDictionary:(NSDictionary*)json { 

@try { 
    label.text = [NSString stringWithFormat:@"%@ from %@", 
        json[@"docs"][0][@"title"], 
        json[@"docs"][0][@"edition"], 
        //json[@"loans"][0][@"loan_amount"], 
        //json[@"loans"][0][@"use"], 
        nil 
        ]; 
} 
@catch (NSException *exception) { 
    [[[UIAlertView alloc]initWithTitle:@"Error" 
           message:@"Could not parse the json feed." 
           delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]show]; 
    NSLog(@"Exception: %@", exception); 
} 

當我運行PROGRAMM那麼IPhone模擬器無法顯示的數據。

我沒有任何想法,哪裏出問題了!?!

有沒有人有想法或解決方案?

+0

你檢查過你的JSON響應嗎?將錯誤傳遞給JSON序列化可能會被破壞。添加NSLog(「Response dictinary%@」,json);到你的更新方法 – Injectios 2014-09-03 17:25:11

+0

轉到json.org並學習JSON語法。它只需要5-10分鐘。 – 2014-09-03 17:26:11

+1

而且**使用NSJSONSerialization的'error:'parm **! – 2014-09-03 17:27:04

回答

2

首先,例外是​​編程錯誤。你不會發現異常。如果你得到異常,你的程序崩潰,你找到原因,你修復代碼。 Objective-C就是這樣做的。人們可能會認爲你是一名Java程序員。

其次,你收到的字典的NSLog將是有用的。您打印的內容與您的代碼不符。

第三,當您獲取JSON數據時,您只需檢查一切。你檢查你是否收到過字典。你檢查是否有一個名爲「docs」的屬性,它是一個數組。你檢查它是否至少有一個元素。你得到第一個元素並檢查它是否是字典。您檢查字典是否有屬性「title」和屬性「edition」,並且它們是字符串等。第四,爲了幫助保持理智,請定義一個對象來表示JSON數據中的內容,將字典作爲初始化工具,並將所有內容提取到該對象中。所以從那時起你就處於安全的NSObject領域。

第五,當用戶閱讀「無法解析JSON訂閱源」時,您認爲會發生什麼?他們可憐的大腦會爆炸。錯誤消息是針對用戶的,而不是針對程序員的。

+1

不錯的一個:)特別是你的最後一點:D – HAS 2014-09-03 17:56:21

+0

我知道你聽不到它,但我現在讚揚你的答案 – 2014-09-04 08:58:15

相關問題