没有在 Xcode 中调用 didReceiveData

Posted

技术标签:

【中文标题】没有在 Xcode 中调用 didReceiveData【英文标题】:didReceiveData not getting called in Xcode 【发布时间】:2012-04-05 20:07:12 【问题描述】:

NSURL 委托的 didReceiveData 方法没有被调用,但是 didReceiveResponse 和 didFinishLoading 被调用了。我浏览了许多在线帖子,但无法获得关于为什么我没有收到任何数据的任何线索。有人可以告诉我我在代码中发生了什么。任何帮助表示赞赏。这是我的代码:

- (IBAction)buttonClicked:(id)sender 
NSString *soapMsg = 
[NSString stringWithFormat:
 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xlmns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
 "<soap:Body>\n"
 "<GetAllCarsJson xmlns=\"http://cscserver2.carrollu.edu/tshah2/\">\n"
 "</GetAllCarsJson>\n"
 "</soap:Body>\n"
 "</soap:Envelope>"]; 

//---print it to the Debugger Console for verification---
//NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:@"http://cscserver2.carrollu.edu/tshah2/CarService.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

//---set the headers---
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://cscserver2.carrollu.edu/tshah2/GetAllCarsJson" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

//[activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[conn start];

NSLog(@"Connection = %@", conn);
if (conn) 

    //webData = [[NSMutableData data] ];
    webData = [[NSMutableData alloc] initWithCapacity:2048];
    //[[NSMutableData data] retainArguments];



- (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response

    NSLog(@"soapMsg1: %d", [webData length]);
    //webData = [[NSMutableData alloc] initWith:
    [webData setLength:0];


- (void) connection:(NSURLConnection *) connection didReceiveData:(NSData *)data

    NSLog(@"DONE1111");
    [webData appendData:data];
    //NSLog(webData);



- (void) connection:(NSURLConnection *) connection didFailWithError:(NSError *)error

    NSLog(@"ConnectionFailed");
    //[webData release];
    //[connection release];


- (void) connectionDidFinishLoading:(NSURLConnection *) connection

    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:             [webData length] encoding:NSUTF8StringEncoding];

    //shows the XML
    NSLog(theXML);
    //[theXML release];
    //[activityIndicator stopAnimating];
    //[connection release];
    connection = nil;
    webData =nil;



- (void) dealloc

    //[activityIndicator release];
    //[xmlParser release];
    //[soapResults release];
    //[super dealloc];

【问题讨论】:

stage:你可能会得到一个空的身体?所以只有一个标题。可能吗? didReceiveResonse 的 NSLog 是soapMsg1: 0。您能否澄清一下您的回复? 【参考方案1】:

检查 http 响应代码。 使用 NSHTTPURLResponse 类的 - (NSInteger)statusCode 方法。是200还是其他?

【讨论】:

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) 响应; int responseStausCode = [httpResponse statusCode]; NSLog(@"code: %d", [httpResponse statusCode]; 我得到的状态码是 400。 对于成功的 HTTP 回复,响应代码应为 2xx。 400 可能意味着服务器认为您的请求格式不正确。

以上是关于没有在 Xcode 中调用 didReceiveData的主要内容,如果未能解决你的问题,请参考以下文章

在 Xcode 中,有没有办法在调用 NSLog 时禁用调试器控制台中出现的时间戳?

有没有办法从 Xcode 调试器的调用堆栈中删除内联函数?

Xcode错误调用-display没有效果

Xcode https 调用 Restful API

在 Xcode 中调用“touches-cancelled”方法

Xcode 9.3 使用 URL 方案(深度链接)调用时构建崩溃 [重复]