Я пытаюсь использовать http POST для отправки объекта JSON (UIImage включен в POST). Ниже приведен код, который я сейчас использую, но по какой-то причине сервер не получает POST. Кто-нибудь может объяснить, почему это может не сработать?
NSString *userString = [[NSString alloc]init];
userString = [[NSUserDefaults standardUserDefaults]valueForKey:@"userId"];
//convert image to nsdata object
NSData *imageData = UIImageJPEGRepresentation(imageView.image, .9);
NSLog(@"User id is:%@", userString);
NSLog(@"The tag string:%@", myTagString);
NSLog(@"the question string is:%@", myQuestionString);
NSLog(@"the image data is:%@", imageData);
NSArray *keys = [NSArray arrayWithObjects:@"category", @"question", @"latitude", @"longitude", @"user_id", @"image",nil];
NSArray *objects = [NSArray arrayWithObjects:myTagString, myQuestionString, @"0.0", @"0.0", userString, imageData, nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSURL *theURL = [NSURL URLWithString:@"http://theserver.com/query"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
NSString *theBodyString = [[NSString alloc]init];
theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary];
NSLog(@"body string: %@", theBodyString);
NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"body data: %@", theBodyData);
[theRequest setHTTPBody:theBodyData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"the response string:%@", theResponseString);
NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseData error:nil];
NSLog(@"%@", theResponseDictionary);
Это мой первый пост на форуме, поэтому я прошу прощения, если какое-то форматирование неверно. Не стесняйтесь критиковать это, чтобы я мог отправлять лучшие посты в будущем.