2010-12-11 12 views
1

http POSTを使用してJSONオブジェクトを送信しようとしています(POSTにUIImageが含まれています)。以下は現在使用しているコードですが、何らかの理由でサーバーが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:メソッドを見てください。

関連する問題