안녕하세요, 저는 웹 서비스에 HTTP POST 연결을 만드는 데 도움이 필요합니다. 나는 그것을하는 방법에 전혀 몰라요.HTTP POST JSON 메서드
URL의 일부로 필요한 입력 매개 변수를 전달할 때만 NSURLConnection을 만들었습니다. 이제는 웹 사이트와 JSON 본문을 제공하는 것만 큼 다른 것처럼 보입니다.
{
"lastmodified":"String content",
"passengerId":9223372036854775807,
"password":"String content",
"userId":"String content"
}
누구든지이 문제에 대해 의견을 개진 할 수 있습니까? 웹 사이트가 www.myURL/operations/AddItem.com이라고 가정하십시오.
EDIT - 여기에 무슨 문제가 있습니까? 나는이 짧은 일부 값을 통과 있기 때문에
NSError *theError = nil;
NSArray *keys = [NSArray arrayWithObjects:@"userId", @"password", nil];
NSArray *objects = [NSArray arrayWithObjects:userNameTextField.text, passwordTextField.text, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *myJSONString =[jsonDictionary JSONRepresentation];
NSData *myJSONData =[myJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"myJSONString :%@", myJSONString);
NSLog(@"myJSONData :%@", myJSONData);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://153.20.32.74/11AprP306/passenger/item"]];
[request setHTTPBody:myJSONData];
[request setHTTPMethod:@"POST"];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSLog(@"response : %@", theResponse);
NSLog(@"error : %@", theError);
NSLog(@"data : %@", data);
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string: %@", string);
NSDictionary *jsonDictionaryResponse = [string JSONValue];
NSLog(@"dic: %@", jsonDictionaryResponse);
[string release];
[theResponse release];
내가 string: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Request Error</title> <style>BODY {..... 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.
읽고있다가 (& passengerId을 LASTMODIFIED)? 이 요청이 새 계정을 만들 때만이 두 매개 변수가 필요한 이유는 모르겠습니다.
['ASIHTTPRequest'] (http://allseeing-i.com/ASIHTTPRequest). 'appendPostData :'또는'appendPostDataFromFile :'메소드를 사용하여 JSON dict을 추가하십시오. – darvids0n