您好我是ios post方法的新手。在我的应用程序中,我想显示值列表。 请求格式为:
{"customerId":"000536","requestHeader":{"userId":"000536"}}
我使用的代码是:
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);
}
}
];
回复是:(null)
如何使用上面显示的相同格式请求值?提前感谢。
答案 0 :(得分:1)
使用此代码
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];