2012-08-09 178 views
0

我有一個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

回答

1

迴應說,它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