2013-05-03 2 views
-3

.NET 서버에 게시 요청이 작동하지 않습니다.iOS의 JSON 게시가 작동하지 않습니다 (.NET 서버)

{"CreatedBy":"","EmailAddress":"[email protected]","Zipcode":"","FirstName":"devan","DeviceCategoryID:":"","State":"","CustomerTypeID:":"",..........} like this..... 

하지만 JSON 개체는 다음과 같아야합니다 :

NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSURL *postURL = [NSURL URLWithString: @"SOME URL"]; 
NSError *error=nil; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error]; 


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: postURL 
                 cachePolicy: NSURLRequestUseProtocolCachePolicy 
                timeoutInterval: 60.0]; 

[request setHTTPMethod: @"POST"]; 
[request setValue: @"application/json" forHTTPHeaderField: @"Accept"]; 
[request setValue: @"application/json" forHTTPHeaderField: @"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody: jsonData]; 

NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData 
               encoding:NSUTF8StringEncoding]); 

출력은 내가 게시에 대한 닷넷 서버를 사용하고

({CreatedBy:"",EmailAddress:"[email protected]",Zipcode:"",FirstName:"devan",DeviceCategoryID::"",State:"",CustomerTypeID:"" }) 

.

요청은 서버에 게시되지 않습니다. 누구든지 나를 도와주세요.

+1

출력이 올바르게 표시됩니다. 10 분을 소비하고 [JSON 형식] (http://www.json.org)을 배우십시오. 서버 측에서이 json을 구문 분석 할 수없는 경우 문제는 다른 곳에 있습니다. – Mar0ux

+1

언제든지 http://json.parser.online.fr/을 사용하여 출력을 확인할 수 있습니다. – Mar0ux

답변

0
NSString *urlString = @"Your Web Service URL"; 

NSString *parameterString = @"XYZ"; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

NSMutableData *body = [NSMutableData data]; 

// parameter 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:parameterString dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

// close form 
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 


// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

// now lets make the connection to the web 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

NSLog(@"returnString %@",returnString);