Receiving twice response when an HTTP request is made

472 views Asked by At

I am making a HTTP call to the server using the POST method.When the register button is clicked am calling the service as given below

 NSURL * url=[NSURL URLWithString:[NSString         stringWithFormat:@"http://offers2win.com/api/v1/users?user[email]=%@&user[password]=%@&user[password_confirmation]=%@",username,password,confirmpassword]];
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

And saving the responde data in the dictionary

 NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
returnString  = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[returnString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];

But the delegate is called twice

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
 NSLog(@"Recieved");
}

What could be the reason?And also it is taking a long time to get the response.

1

There are 1 answers

0
CRDave On

This is not error. Apple has already told about this in NSURLConnectionDataDelegate Protocol Reference:

In rare cases, for example in the case of an HTTP load where the content type of the load data is multipart/x-mixed-replace, the delegate will receive more than one connection:didReceiveResponse: message. In the event this occurs, delegates should discard all data previously delivered by connection:didReceiveData:, and should be prepared to handle the, potentially different, MIME type reported by the newly reported URL response.

Check Here : NSURLConnectionDataDelegate Protocol Reference