0

我有一个 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);

我尝试了两种方法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>

我很感激任何帮助。

4

1 回答 1

1

回复说,它不喜欢这样

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

所以它不提供用 json 响应。

Web 服务的文档应该列出所有可能的值。

你可能想试试

[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];

或者重新配置 tomcat 以接受 json 作为响应格式。

于 2012-08-09T16:36:37.090 回答