2013-05-10 14 views
13

Per inviare i dati di registrazione al server, sto usando JSON è nella forma seguente:Invia nidificati JSON utilizzando AFNetworking

Ecco come mando.

NSURL * url = [[NSURL alloc] initWithString:registerUrlString]; 
      AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
      httpClient.parameterEncoding = AFJSONParameterEncoding; 
      [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; 
      NSDictionary * params = @{@"regData": @{ 
               @"City": self.cityField.text, 
               @"Country": self.countryField.text, 
               @"Email_Id": self.emailField.text, 
               @"MobileNumber": self.numberField.text, 
               @"UserName": self.userName.text, 
               } 
             }; 

      NSMutableURLRequest * request = [httpClient requestWithMethod:@"POST" path:registerUrlString parameters:params]; 
      AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
       NSLog(@"Success: %@", JSON); 

      } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
      NSLog(@"Error: %@", [error debugDescription]); 
      }]; 

      [operation start]; 

Ma purtroppo sto ottenendo questo errore:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x94b3c30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

+1

il tuo metodo non è generico. Controlla il metodo corretto [qui] (http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class). Meno soggetti a errori e mantenibili –

risposta

32

La richiesta va bene. L'errore Error Domain=NSCocoaErrorDomain Code=3840 viene restituito perché il server risponde con un oggetto JSON non valido. NSLogoperation.responseString per vedere cosa viene inviato indietro.

+2

Grazie, mattt! Ho già risolto questo problema. Il problema era come hai menzionato. Stavo ricevendo una stringa dal server piuttosto un json. – Homam

+1

@mattt, ho sottoclasse, 'AFHTTPSessionManager' dove posso trovare' operation.responseString'? – Hemang

+0

@Hemang hai trovato qualche soluzione relativa a "Errore dominio = NSCocoaErrorDomain Code = 3840"? – Akhtar

2

Prova questo per ottenere l'errore effettivo

NSLog(@"Error: %@", [error debugDescription]); 
NSLog(@"Error: %@", [error localizedDescription]); 
+4

Ricevo questo errore: Errore Dominio = NSCocoaErrorDomain Code = 3840 "Impossibile completare l'operazione. (Errore di cacao 3840.)" (Il testo JSON non iniziava con l'array o l'oggetto e l'opzione per consentire frammenti non impostati.) UserInfo = 0x94b3c30 {NSDebugDescription = Il testo JSON non è stato avviato con array o oggetto e opzione per consentire frammenti non impostati.} – Homam