我正在尝试使用以下参数调用 Web 服务:
{"Class":"Authorization","method":"login","user":"","pass":""}
如下
NSString *user = [NSString stringWithFormat:@"username=%@",_username.text];
NSString *pass = [NSString stringWithFormat:@"password=%@",_password.text];
NSString *Class = @"Authorization";
NSString *method = @"login";
//NSString *post = [NSString stringWithFormat:@"%@&%@&%@&%@", user, pass, Class, method ];
NSString *jsonPostBody = [NSString stringWithFormat:@"'json' = '{\"Class\":"
"\"%@\""
",\"method\":"
"\"%@\""
",\"pass\":"
"\"%@\""
",\"user\":"
"\"%@\""
"}'",
[Class stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[method stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[pass stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [jsonPostBody dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSURL *url = [NSURL URLWithString:@"http://212.119.87.45:8080/IktissabServices/index.php/service"];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:180.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[[jsonPostBody stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]
dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES]];
[request setHTTPBody:postData];
NSString* postDataLengthString = [[NSString alloc] initWithFormat:@"%d", [postData length]];
[request setValue:postDataLengthString forHTTPHeaderField:@"Content-Length"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];//
if (! error) {
NSLog(@"%@",jsonDict);
}else{
NSLog(@"%@",error.localizedDescription);
}
每次结果都是:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“数据参数为零”
网络服务有:
输入内容类型:文本/json
输出内容类型:文本/json
任何帮助或示例将不胜感激。