私は、mySQL データベース、iPhone クライアント、RESTful Web サービス (jersey を使用) をそれらの間の中間層として使用しています。データベースに正常に接続し、GET
リクエストを実装しました。私はそれに問題がPOST
あります。
このようにして、転送する JSON を準備します。
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);
の 2 つの方法を試しPOST
ましたが、いずれも成功しませんでした。
1) 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) 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];
コードにもこのメソッドがあります:
- (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);
}
コンソールで次のエラーが発生しました。
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>
助けてくれてありがとう。