在iphone中发布json请求

问题描述:

我有一个mySQL数据库,一个iPhone客户端和一个RESTful Web服务(使用球衣)作为它们之间的中间层。我成功连接到数据库并实施了一个GET请求。我对POST有问题。在iphone中发布json请求

这样,我准备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,但其中的非成功。使用JSON的NSString

1)

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> 

我很感激任何帮助。

+0

没有东西跳出来对我。这里究竟发生了什么问题?你看到任何错误?没有看到请求打到服务器?没有得到回应?崩溃? – warrenm 2012-08-09 16:28:49

+0

我在最后加了错误信息。 – Ali 2012-08-09 16:34:58

回应说,它doenst这样

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

所以doenst报价与JSON响应。

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

你可能想尝试

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

或重新配置Tomcat能够接受JSON作为响应格式。

+0

'{“name”:“jkkh”,“password”:“kjh”,“mobil”:“kjh”,“birth”:“1981-01-01”,“city”:“kjhkj” :“kjhh”}' – Ali 2012-08-09 16:41:05

+0

这是客户端中json字符串的打印。你认为它有什么问题吗? – Ali 2012-08-09 16:41:38

+0

这是,你想要发送到服务器。这可能是好的。但是你告诉tomcat,客户端正在接受json,但是tomcat似乎无法响应json – vikingosegundo 2012-08-09 16:47:20