【发布时间】:2014-10-12 18:46:48
【问题描述】:
我正在关注这些问题和教程:
- fetching data with get post methods
- sending http get and post from cocoa
- nsurlconnection basics for beginners
- sending an http post request on ios
- how to hanlde multiple delegates
但我仍然缺少一些东西。我的委托方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
一直被调用,需要一点时间,我猜是因为网速,但是方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
永远不会被调用。我错过了什么?请帮帮我。
我的代码:
-(void)loadDataBase{
NSString *post = [NSString stringWithFormat:@"advnr=123"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://thewebsite.abc/interface.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
//return @"";
}
#pragma mark -
#pragma mark NSURLConnection delegates
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data {
NSLog(@"Success"); // never gets executed
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"didFinished");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"FAILED");
}
【问题讨论】:
-
这个委托方法只在webservice返回数据时调用,你确定你的post请求返回的数据不是200个http头吗?
-
是的,当我使用我的 html 测试表单对其进行测试时,我总是会在屏幕上获得我需要的数据。