我試圖調用這些參數的Web服務:IOS HTTP請求發送JSON格式
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);
}
每次
{"Class":"Authorization","method":"login","user":"","pass":""}
如下結果:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
網絡服務有:
input-content-type: text/json
output-content-type: text/json
任何幫助或例子將不勝感激。
錯誤是什麼? – woz
當通過登錄測試並調用web服務時,語句會出現終止應用程序,由於未捕獲的異常'NSInvalidArgumentException',原因:'數據參數爲零' –
它返回給你結果?如何和它從我身邊終止親切送我結果 –