开发者

HTTP POST JSON Method

开发者 https://www.devze.com 2023-04-01 02:26 出处:网络
Hi I need help in making a HTTP POST connection to a web service. I am totally clueless on how to do it.

Hi I need help in making a HTTP POST connection to a web service. I am totally clueless on how to do it.

I only had experienced in making a NSURLConnection whereby I pass the required input parameters as part of the URL. Now it seems different as I am only provided a website and the request JSON body.

{
"lastmodified":"String content",
"passengerId":9223372036854775807,
"password":"String content",
"userId":"String content"
}

Could anyone shed some light on this? Say the website is www.myURL/operations/AddItem.com

EDIT - What am I doing wrong here?

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

I am reading 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'.

Is it because I have short passed some values(lastmodified & passengerId)? I have no idea why do they need these 2 parameters as this request is only creating a new account.


You could use ASIHTTPRequest as @darvids0n suggests, or you could also do it using the standard iOS NSMutableURLRequest method as follows:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:myURL]; // Assumes you have created an NSURL * in "myURL"
[request setHTTPMethod:@"POST"];
[request setHTTPBody:myJSONData]; // Assumes you have created an NSData * for your JSON
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

However you choose to do it, you will need to convert your Objective-C object(s) into a JSON string and then convert that into an NSData. Here is an example of how to do both those steps at once using the SBJSON framework.

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSData *myJSONData = [writer dataWithObject:myDataDictionary];

The above converts the dictionary into a JSON string and then converts that string into an NSData using UTF8 encoding of the string. If you want a different encoding you will have to break use [writer stringWithObject:myDataDictionary] and encode it yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号