【问题标题】:Why my delegate method never gets called?为什么我的委托方法永远不会被调用?
【发布时间】:2014-10-12 18:46:48
【问题描述】:

我正在关注这些问题和教程:

但我仍然缺少一些东西。我的委托方法

- (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 测试表单对其进行测试时,我总是会在屏幕上获得我需要的数据。

标签: ios delegates http-post


【解决方案1】:

我怀疑确实没有返回数据(可能是因为某些 Web 服务错误或准备不正确的请求)。

如果您愿意,可以使用Charles 观察请求和响应以确认这一点。顺便说一句,Charles 或任何类似的工具都是添加到您的武器库中的有用武器,因为它极大地简化了 HTTP 问题的诊断,将网络相关问题与客户端编程问题隔离开来。

无论如何,如果您遇到此类错误,您通常会收到服务器返回的非 200 statusCode。见list of HTTP status codes。

您还可以通过实现didReceiveResponse 来验证这一点并检查响应的标头:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
        int statusCode = [(NSHTTPURLResponse *)response statusCode];
        if (statusCode != 200) {
            NSLog(@"Status code = %ld; response = %@", (long)statusCode, response);

            // handle this error however you'd like
        }
    }

    // temporarily, you might even want to log the whole `response` regardless of the status code
    NSLog(@"%s: %@", __PRETTY_FUNCTION__, response);
}

【讨论】:

  • 我得到了{ status code: 200, headers { Connection = close; "Content-Length" = 0; "Content-Type" = "text/html"; Date = "Sun, 12 Oct 2014 19:21:08 GMT"; Server = "Apache/2.2.22"; "X-Powered-By" = "PHP/5.3.26"; } },但我不明白为什么...如果我使用 html 测试表单,我总是会得到我的数据..
  • 这令人鼓舞。我们想看200。所以下一个问题是服务器是否真的没有返回任何东西。这就是查尔斯变得无价的地方。
  • 内容长度为 0... 这意味着我没有得到任何数据,对吧?
  • 是的。这让我怀疑是服务器问题或 API 问题。
  • 是的,有时会涉及到 cookie。或者字段名称拼写错误。或者服务器正在寻找的一些隐藏字段。在某些情况下,Web 服务实际上是在寻找 User-Agent 标头字段(以确认它是 Web 浏览器)。很难说没有看到 HTML。我有没有提到查尔斯在这里很有用(观看网络浏览器请求与应用程序请求并进行比较)?哈哈。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2013-07-22
相关资源
最近更新 更多