2016-08-09 69 views
0

Hallo, ich bin neu zu IOS Post-Methode.In meiner App möchte ich Liste der Werte zeigen. Das Anforderungsformat ist:So fordern Sie Daten im Post Method Ios zum Server an?

{"customerId":"000536","requestHeader":{"userId":"000536"}} 

Der Code i wird verwendet:

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    NSString *post =[[NSString alloc] initWithFormat:@"customerId=%@&userId=%@",@"000536",@"000536"]; 
    NSLog(@"PostData: %@",post); 

    NSURL *url=[NSURL URLWithString:@"https://servelet/URL"]; 
    NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
           @"000536", @"customerId", 
           @"000536", @"userId", 
           nil]; 

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

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:postData]; 

    //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error){ 
     // NSLog(@"Response code: %ld", (long)[response statusCode]); 
     if(error || !data){ 
      NSLog(@"Server Error : %@", error); 
     } 
     else 
     { 
      NSLog(@"Server Response :%@",response); 
      NSError* error; 
      NSDictionary* json = [NSJSONSerialization 
            JSONObjectWithData:data 
            options:kNilOptions 
            error:&error]; 

      NSArray* latest = [json objectForKey:@"apptModel"]; 
      NSLog(@"items: %@", latest); 
     } 
    } 
    ]; 

Die Antwort ist: (null)

Wie die Werte mit dem gleichen Format anzufordern, wie oben gezeigt Dank im Voraus.

+1

Fragen Sie Ihren Server, warum es eine leere Antwort zurück. Oder prüfen Sie, was die Antwort ist und ob JSON gültig ist. – Avi

+0

Aus Ihrer Anfrage Format {"customerId": "000536", "requestHeader": {"userId": "000536"}} Ich habe festgestellt, dass customerId und UserId beide denselben Wert haben. So müssen Sie Server Kerl bitten, Anfrage für das gleiche zu aktualisieren. Und andere Sache, die Sie möglicherweise userId als ** Request Header ** instaedof hinzufügen, dass in Anfrage Wörterbuch – Bhavik

+0

Antwort Ich bin in Post Mann als "{" responseHeader ": {" responseCode ": 0," responseMessage ": "Erfolg"}, "apptModel": [{"customerid": "000536", "perfomerid": "000004", "Name": "Dr", "Nachname": "abc", "Geschlecht": "MALE" , "Spezialisierung": "abc", "Beschreibung": "abc"}, "" ...... @ Avi @Bhavik – Vignesh

Antwort

1

Verwenden Sie diesen Code

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
NSString *post =[[NSString alloc] initWithFormat:@"customerId=%@&userId=%@",@"000536",@"000536"]; 
NSLog(@"PostData: %@",post); 

NSURL *url=[NSURL URLWithString:@"https://servelet/URL"]; 
NSDictionary *jsonDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
          @"000536", @"customerId", 
          @"000536", @"userId", 
          nil]; 

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

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json; character=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 
NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *err) 
{ 
    // NSLog(@"Response code: %ld", (long)[response statusCode]); 
    if(error || !data){ 
     NSLog(@"Server Error : %@", error); 
    } 
    else 
    { 
     NSLog(@"Server Response :%@",response); 
     NSError* error; 
     NSDictionary* json = [NSJSONSerialization 
           JSONObjectWithData:data 
           options:kNilOptions 
           error:&error]; 

     NSArray* latest = [json objectForKey:@"apptModel"]; 
     NSLog(@"items: %@", latest); 
    } 
}]; 
[task resume]; 
+0

was ist die Änderung @PinkeshGjr. Ich bekomme immer noch (Null) Wert. – Vignesh

+0

Debug und dann überprüfen generierte URL funktioniert im Browser – PinkeshGjr

+0

Ja funktioniert es im Browser --PinkeshGjr. Finden Sie über Kommentare und Antworten, die ich gezeigt habe. – Vignesh