2017-06-01 2 views
0

Ich versuche, JSON-Daten zu holen und analysiere es NSURL Sitzung verwenden, aber jedes Mal, null bekommen - auch zusätzlich zu dem, i o zeigen will, dass alle Daten in der TabellenansichtWie Daten mit NSUrl-Sitzung in Objective-C abrufen?

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]; 

ich etwas wissen Darf ich falsch machen ist alles andere eine gute Möglichkeit, dasselbe zu tun?

+0

Was ist null? Antwort? "Daten"? 'jsonDict'? Normalerweise setzen Sie keine Parameter für GET in HTTPBody. – Larme

Antwort

0

Versuchen Sie den folgenden Code.

+0

Haben Sie ein Beispiel dafür, wie ich Tabellenansicht programmaticaly erstellen und Daten von JSON-Antwort in der Tabellenansicht mit Bildern in asynchronsly – sss

+0

anzeigen sollte Sie es googeln für das ... :) aber hier ist hilfreich Link https://developer.apple .com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html –

0

Bitte verwenden Sie den folgenden Code für das gleiche. Hoffe es wird dein Problem lösen.

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

Die Frage wird nach 'NSURLSession' gestellt, nicht nach' NSURLConnection' –