내가 AFNetworing
와 JSON
요청을 보내려고하지만 나에게 JSON text did not start with array or object and option to allow fragments not set
오류를주는 코드를 다음하고는 :잘못된 유형 (NSConcreteMutableData는)
{
password = q;
request = login;
userName = q;
}
:
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
객체의 로그 출력
오류에 대한 비슷한 질문을 보았습니다. 배열에 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 :
나는 다음과 같은 시도했지만이 작업 did't :
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;
} }>
: 내 complate
services.php
파일을 전달하고AFNetworiking
및 PHP 서비스와JSON
얻기위한 complate 예로 만들기 위해 여기에그리고 나의 complate 작업 코드는 여기 매개 변수로 전달하는 빌드 된 NSString이며 유효합니다. '{ "request": "login", "userName": "q", "password": "q"}' –
@ OlcayErtaş 문자열에서 슬래시가 여전히 동일한 오류를 생성합니까? 수행 할 수있는 수표로 답변을 업데이트했습니다. 당신이 이것을 이미 본지 확실하지 않은 경우 : http://stackoverflow.com/questions/21452385/json-text-did-not-start-with-array-or-object-and-option-to-allow-fragments-not- 에스 – Giwan