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.
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
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
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