2010-12-11 3 views
1

HTTP POST를 사용하여 JSON 객체 (UIImage가 POST에 포함됨)를 보내려고합니다. 아래는 현재 사용중인 코드입니다. 그러나 어떤 이유로 서버가 POST를받지 못합니다. 왜 이것이 작동하지 않는지에 대한 통찰력을 제공 할 수 있습니까?HTTP POST UIImage 용 JSON 사용

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); 

포럼의 첫 번째 게시물이기 때문에 형식이 잘못되어 사과를드립니다. 앞으로도 더 나은 게시물을 제출할 수 있도록 비판 해보십시오.

답변

0

Github http://akos.ma/7qp에있는이 프로젝트의 코드를 살펴 보겠습니다. 여기서 Wrapper 클래스는 요청에 몇 가지 헤더를 설정하여 서버가 업로드되는 바이너리 데이터를 처리 할 수 ​​있도록합니다. 필수 콘텐츠 유형 헤더를 설정하는 118 행의 uploadData : toUrl : 메소드를 확인하십시오.