I have a mySQL database, an iPhone client and a RESTful Web Service(using jersey) as an intermediate layer between them. I connected successfully to the database and implemented a GET Request. I have problem to POST to it.
In this way I prepare JSON to transfer:
NSDate *date = self.pickerView.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *stringFromDate = [formatter stringFromDate:date];
[formatter release];
NSArray *keys = [NSArray arrayWithObjects:@"name", @"mail",@"password",@"mobil", @"birth", @"city" , nil];
NSArray *objects = [NSArray arrayWithObjects:name.text, mail.text, password.text, mobil.text, stringFromDate, city.text, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@", jsonString);
I tried two ways to POST, but non of them was successful.
1) using JSON NSString:
NSURL *url = [NSURL URLWithString:@"http://192.168.1.100:8080/rest/login/post"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *requestData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
2) using NSData:
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.100:8080/rest/login/post"]];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:jsonData];
NSURLConnection * myConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
I have this method also in my codes:
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableData *d = [[NSMutableData data] retain];
[d appendData:data];
NSString *a = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding];
NSLog(@"Data: %@", a);
}
I got this error in Console:
Data: <html><head><title>Apache Tomcat/7.0.23 - Error report</title><style><!--
H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-
size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-
color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-
serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-
family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-
family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-
family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color :
black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status
406 - Not Acceptable</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p>
<b>message</b> <u>Not Acceptable</u></p><p><b>description</b> <u>The resource identified by
this request is only capable of generating responses with characteristics not acceptable
according to the request "accept" headers (Not Acceptable).</u></p><HR size="1"
noshade="noshade"><h3>Apache Tomcat/7.0.23</h3></body></html>
I appreciate for any help.
The response says, that it doenst like this
So it doenst offer to respond with json.
The documentation of the web service should list all possible values.
You may want to try
Or reconfigure tomcat to accept json as response format.