didReceiveData not getting called in Xcode

Go To StackoverFlow.com

1

NSURL delegate didReceiveData method is not getting called but didReceiveResponse and didFinishLoading is getting called. I went through many online posts, but not able to get any clue on why I am not receiving any data. Can someone tell me what I am going on in the code. Any help is appreciated. Here is my code:

- (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];
}
2012-04-05 20:07
by vvg
strage: you might get a empty body back? so only a header. possible - Jonas Schnelli 2012-04-06 15:17
NSLog for didReceiveResonse is soapMsg1: 0. Also can you clarify on your reply - vvg 2012-04-06 15:42


0

Check the http response code. using - (NSInteger)statusCode method of NSHTTPURLResponse Class. Is it 200 or other?

2012-04-06 15:21
by Tech Agent
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; int responseStausCode = [httpResponse statusCode]; NSLog(@"code: %d", [httpResponse statusCode]; I am getting status code as 400 - vvg 2012-04-06 16:13
For a successful HTTP reply, response code should be 2xx. 400 may mean that server thinks that your request is not properly formatted - Tech Agent 2012-04-07 07:08
Ads