2017-06-01 46 views
0

我試圖獲取JSON數據並使用NSURL會分析它,但得到空每一次 - 而且除了我想o顯示在表視圖中的所有數據如何在Objective-C中使用NSUrl會話獲取數據?

NSError *error; 

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
    NSURL *url = [NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0]; 

    [request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:@"text/plain" forHTTPHeaderField:@"Accept"]; 

    [request setHTTPMethod:@"GET"]; 


    NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"Value", @"Key", nil]; 

    NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error]; 

    [request setHTTPBody:postData]; 


    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 


     NSLog(@"Resopnse == %@",response); 

     if (response != nil) { 
      NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

      NSLog(@"JSon dict === %@",jsonDict); 
     } 

    }]; 

    [postDataTask resume]; 

我可以知道我做錯了有什麼其他的好方法可以做同樣的事嗎?

+0

什麼是空? '迴應'? '數據'? 'jsonDict'?通常,您不要將GET的參數放入HTTPBody中。 – Larme

回答

0

請嘗試下面的代碼。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json"]]; 
    [request setHTTPMethod:@"GET"]; 
    [request addValue:@"text/plain" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:@"text/plain" forHTTPHeaderField:@"Accept"]; 

     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
     [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
     NSData * responseData = [requestReply dataUsingEncoding:NSUTF8StringEncoding]; 
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 
     NSLog(@"requestReply: %@", jsonDict); 
    }] resume]; 
+0

你有任何例子,我應該如何創建表格視圖programmaticaly和顯示數據從JSON響應tableview與圖像在異步 – sss

+0

你應該谷歌它的:)但這裏是有用的鏈接https://developer.apple .com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html –

0

請使用下面的代碼。希望它能解決你的問題。

NSString *strUrl = [NSString stringWithFormat:yourUrl]; 
    NSURL *url = [NSURL URLWithString:strUrl]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, 
               NSData *data, NSError *connectionError) 
    { 
     if (data.length > 0 && connectionError == nil) 
     { 
      NSDictionary *dicResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; 
      NSLog(@"%@",dicResponse); 
     } 
    }]; 
+0

問題需要提供'NSURLSession',而不是'NSURLConnection' –

相關問題