我試圖發送一個JSON
請求與AFNetworing
但下面的代碼給我JSON text did not start with array or object and option to allow fragments not set
錯誤:(NSConcreteMutableData)無效類型的JSON寫
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]);
}];
我在做什麼錯了?
UPDATE:
我曾嘗試以下,但我以前不工作:
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;
} }>
@givan這是我編寫的NSString參數,它是有效的:'{「request」:「login」,「userName」:「q」,「password」:「q」}' – 2015-01-21 09:06:44
@OlcayErtaş運行代碼在字符串中斜線仍然會產生相同的錯誤?我已經用可以執行的檢查更新了我的答案。不知道你是否看到這個:http://stackoverflow.com/questions/21452385/json-text-did-not-start-with-array-or-object-and-option-to-allow-fragments-not-小號 – Giwan 2015-01-21 10:49:19