我正在尝试使用JSON
发送AFNetworing
个请求,但是后面的代码却给我JSON text did not start with array or object and option to allow fragments not set
错误:
NSString *post = [[NSString alloc] initWithFormat:
@"{\"request\":\"login\",\"userName\":\"%@\",\"password\":\"%@\"}", userName, password];
NSData *parameters = [post dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *parameterDictionary =
[NSJSONSerialization JSONObjectWithData:parameters options:NSJSONReadingAllowFragments error:nil];
DDLogDebug(@"Data: %@", [parameterDictionary description]);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:parameterDictionary
success:^(AFHTTPRequestOperation *operation, id responseObject) {
DDLogDebug(@"LoginView - Success Response: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError(@"LoginView - Error Response: %@", [error description]);
}];
[operation start];
这是parameterDictionary
对象的日志输出:
{
password = q;
request = login;
userName = q;
}
我看了类似的错误问题,并尝试将parameters
对象放入数组中,但这次我收到了错误" Invalid type in JSON write (NSConcreteMutableData)
"
NSMutableArray *array = [NSMutableArray new];
[array addObject:parameterDictionary];
DDLogDebug(@"Data: %@", [parameterDictionary description]);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:array
success:^(AFHTTPRequestOperation *operation, id responseObject) {
DDLogDebug(@"LoginView - Success Response: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError(@"LoginView - Error Response: %@", [error description]);
}];
我做错了什么?
更新:
我尝试了以下但是确实没有工作:
NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"login" forKey:@"request"];
[dictionary setObject:@"q" forKey:@"userName"];
[dictionary setObject:@"q" forKey:@"password"];
DDLogDebug(@"Dictionary: %@", [dictionary description]);
DDLogDebug(@"Json: %@", [dictionary JSONRepresentation]);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:dictionary
success:^(AFHTTPRequestOperation *operation, id responseObject) {
DDLogDebug(@"LoginView - Success Response: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError(@"LoginView - Error Response: %@", [error description]);
}];
更新2:
这是AFHTTPRequestOperation
在错误块上看起来的样子:
<AFHTTPRequestOperation: 0x7fd881fa1200, state: isFinished, cancelled: NO request: <NSMutableURLRequest: 0x7fd881f9fd60> { URL: http://www.olcayertas.com/services.php }, response: <NSHTTPURLResponse: 0x7fd881d913b0> { URL: http://www.olcayertas.com/services.php } { status code: 200, headers {
Connection = close;
"Content-Length" = 1050;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 21 Jan 2015 10:28:45 GMT";
Server = Apache;
"X-Powered-By" = PleskLin;
} }>
答案 0 :(得分:0)
JSON似乎没有在JSONlint.com上验证。我会首先确保JSON是正确的。
这是我输入的内容:
{\"request\":\"login\",\"userName\":\"%@\",\"password\":\"%@\"}
我用它来检查JSON:
if ([NSJSONSerialization isValidJSONObject:requestJSONContents]) { }
答案 1 :(得分:0)
我通过在浏览器中检查我的网络服务解决了这个问题。问题出在我的services.php文件中。创建日志文件时出错,此错误返回了导致请求失败的非JSON
响应。我的complate工作代码在这里:
NSMutableDictionary *dictionary = [NSMutableDictionary new];
[dictionary setObject:@"login" forKey:@"request"];
[dictionary setObject:@"q" forKey:@"userName"];
[dictionary setObject:@"q" forKey:@"password"];
DDLogDebug(@"Dictionary: %@", [dictionary description]);
DDLogDebug(@"Json: %@", [dictionary JSONRepresentation]);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
//[manager.securityPolicy setAllowInvalidCertificates:true];
AFHTTPRequestOperation *operation =
[manager POST:WEB_SERVICE_URL parameters:dictionary
success:^(AFHTTPRequestOperation *operation, id responseObject) {
DDLogDebug(@"LoginView - Success Response: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError(@"LoginView - Error Response: %@", [error description]);
DDLogError(@"Error: %@",operation);
}];
[operation start]
这是我的complate services.php
文件,使其成为使用JSON
和PHP服务传递和获取AFNetworiking
的比较示例:
PHP web service that accepts JSON input and return JSON response