2015-01-21 105 views
0

我試圖發送一個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; 
} }> 

回答

0

我已經在瀏覽器中檢查我的Web服務解決了這個問題。問題出在我的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文件,使之作爲一個complate例如用於傳遞與AFNetworiking和PHP服務獲得JSON

PHP web service that accepts JSON input and return JSON response

0

JSON似乎並未在JSONlint.com上進行驗證。我會首先確定JSON是正確的。

這是我輸入的內容: {\"request\":\"login\",\"userName\":\"%@\",\"password\":\"%@\"}

我用這個來檢查JSON:

if ([NSJSONSerialization isValidJSONObject:requestJSONContents]) { }

+0

@givan這是我編寫的NSString參數,它是有效的:'{「request」:「login」,「userName」:「q」,「password」:「q」}' – 2015-01-21 09:06:44

+0

@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