NSURLConnection JSON Enviar al server

Aquí estoy perdido, pensé probar algo nuevo con web services para mi aplicación.

Puedo extraer datos sin problemas, pero estoy intentando publicarlos en el server y parece que esto incluso puede disparar.

Lo que tengo la intención de hacer es presionar el button enviar la acción que se activa:

- (IBAction)didPressSubmit:(id)sender { //JSON SERIALIZATION OF DATA NSMutableDictionary *projectDictionary = [NSMutableDictionary dictionaryWithCapacity:1]; [projectDictionary setObject:[projectName text] forKey:@"name"]; [projectDictionary setObject:[projectDescShort text] forKey:@"desc_short"]; [projectDictionary setObject:[projectDescLong text] forKey:@"desc_long"]; NSError *jsonSerializationError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError]; if(!jsonSerializationError) { NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"Serialized JSON: %@", serJSON); } else { NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]); } // JSON POST TO SERVER NSURL *projectsUrl = [NSURL URLWithString:@"http://70.75.66.136:3000/projects.json"]; NSMutableURLRequest *dataSubmit = [NSMutableURLRequest requestWithURL:projectsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [dataSubmit setHTTPMethod:@"POST"]; // 1 [dataSubmit setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2 [dataSubmit setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3 [dataSubmit setHTTPBody: jsonData]; [[NSURLConnection alloc] initWithRequest:dataSubmit delegate:self]; } 

Después de eso, se ejecuta:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"DidReceiveResponse"); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData { NSLog(@"DidReceiveData"); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [UIApplication shanetworkingApplication].networkActivityIndicatorVisible = NO; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"BLAH CHECK YOUR NETWORK" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [errorView show]; [UIApplication shanetworkingApplication].networkActivityIndicatorVisible = NO; } 

Obviamente me falta algo, pero ni siquiera sé dónde mirar. Todo lo que necesito es un punto en la dirección correcta, cualquier ayuda sería genial.

ACTUALIZAR

Pude get la request para disparar con lo siguiente.

De acuerdo, pude get la request para disparar usando lo siguiente:

 - (IBAction)didPressSubmit:(id)sender { NSMutableDictionary *projectDictionary = [NSMutableDictionary dictionaryWithCapacity:1]; [projectDictionary setObject:[projectName text] forKey:@"name"]; [projectDictionary setObject:[projectDescShort text] forKey:@"desc_small"]; [projectDictionary setObject:[projectDescLong text] forKey:@"desc_long"]; NSError *jsonSerializationError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError]; if(!jsonSerializationError) { NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"Serialized JSON: %@", serJSON); } else { NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]); } NSURL *projectsUrl = [NSURL URLWithString:@"http://70.75.66.136:3000/projects.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:projectsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request setHTTPMethod:@"POST"]; // 1 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2 [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3 [request setHTTPBody: jsonData]; // 4 (void) [[NSURLConnection alloc] initWithRequest:request delegate:self]; } 

Pero, por alguna razón, el método de publicación solo recibió un montón de valores nulos, obtengo esto del lado del server. Processing by ProjectsController#create as JSON Parameters: {"{\n \"desc_long\" : \"a\",\n \"name\" : \"a\",\n \"desc_small\" : \"a\"\n}"=>nil}

ACTUALIZACIÓN 2

Con un poco de lectura de aquí: http://elusiveapps.com/blog/2011/04/ios-json-post-to-ruby-on-rails/

Pude ver si se perdió la siguiente línea. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

Entonces, el código final de PressSubmit es el siguiente;

 - (IBAction)didPressSubmit:(id)sender { NSMutableDictionary *projectDictionary = [NSMutableDictionary dictionaryWithCapacity:1]; [projectDictionary setObject:[projectName text] forKey:@"name"]; [projectDictionary setObject:[projectDescShort text] forKey:@"desc_small"]; [projectDictionary setObject:[projectDescLong text] forKey:@"desc_long"]; NSError *jsonSerializationError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError]; if(!jsonSerializationError) { NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"Serialized JSON: %@", serJSON); } else { NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]); } NSURL *projectsUrl = [NSURL URLWithString:@"http://70.75.66.136:3000/projects.json"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:projectsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; [request setHTTPMethod:@"POST"]; // 1 [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3 [request setHTTPBody: jsonData]; // 4 (void) [[NSURLConnection alloc] initWithRequest:request delegate:self]; } 

Tengo el mismo problema, pero lo resolví estableciendo las opciones a cero.

Reemplazar

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError]; 

por

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:nil error:&jsonSerializationError]; 

Utilice la opción nil si está enviando json al server, si está visualizando json, use NSJSONWritingPrettyPrinted .

Espero que esto te ayudará.