如何将Http请求中的json数据发送到JSON解析中的POST方法
How to send json data in the Http request to POST Method in JSON Parsing
我需要将下面的字符串解析为 iOS
中的 POST 方法
"jsonData":{"empId":"cxvd","password":"sfsd"}
但是我得到的错误是
回复:Tomcat错误
HTTP 状态 400 - 所需的字符串参数 'jsonData' 不存在
//------ Method I have used to Parse is ---------- //
+(void) requestToServerForLogin:(NSString*)userName andPassward: (NSString*)password onCompletion:(RequestCompletionHandler) handler
{
NSString *url = [Ip stringByAppendingString:@"login"];
NSString *jsonString = [NSString stringWithFormat:@"\"jsonData\":{\"empId\":\"%@\",\"password\":\"%@\"}",
userName,
password ];
NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:nsurl];
[urlRequest setTimeoutInterval:60.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json"
forHTTPHeaderField:@"Content-type"];
NSString *body = jsonString1;
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"urlRequest :%@",[body dataUsingEncoding:NSUTF8StringEncoding]);
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data1, NSError *error)
{
NSString *res = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
if(handler) handler(res,error);
}];
}
提前致谢
代码太多了。 substringToIndex
和 substringFromIndex
是错误的,不应该在代码中。
对字典使用文字语法:
NSDictionary *jsonDict = @{@"jsonData":@{@"password":password, @"empId":userName}};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];
我需要将下面的字符串解析为 iOS
中的 POST 方法"jsonData":{"empId":"cxvd","password":"sfsd"}
但是我得到的错误是
回复:Tomcat错误
HTTP 状态 400 - 所需的字符串参数 'jsonData' 不存在
//------ Method I have used to Parse is ---------- //
+(void) requestToServerForLogin:(NSString*)userName andPassward: (NSString*)password onCompletion:(RequestCompletionHandler) handler
{
NSString *url = [Ip stringByAppendingString:@"login"];
NSString *jsonString = [NSString stringWithFormat:@"\"jsonData\":{\"empId\":\"%@\",\"password\":\"%@\"}",
userName,
password ];
NSURL *nsurl = [NSURL URLWithString:url];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:nsurl];
[urlRequest setTimeoutInterval:60.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json"
forHTTPHeaderField:@"Content-type"];
NSString *body = jsonString1;
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"urlRequest :%@",[body dataUsingEncoding:NSUTF8StringEncoding]);
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data1, NSError *error)
{
NSString *res = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
if(handler) handler(res,error);
}];
}
提前致谢
代码太多了。 substringToIndex
和 substringFromIndex
是错误的,不应该在代码中。
对字典使用文字语法:
NSDictionary *jsonDict = @{@"jsonData":@{@"password":password, @"empId":userName}};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];