I’m trying to send an asynchronous http request to the server (django in my case).
for some reason it calls the didFailWithError method – which means it doesn’t work.
this is my code:
responseData = [NSMutableData data];
baseURL = [NSURL URLWithString:@"http://localhost:8000/messages/views/new_messages/"];
NSURLRequest *request =
[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8000/messages/views/new_messages/"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
return TRUE;
}
and these are the connected methods:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
NSLog(@"part 1 works");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
NSLog(@"part 2 works");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error!!!");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"finish");
}
- what’s wrong with it?
- after I correct it – how can I send post arguments to the server through this?
thanks
Try replacing your error call with:
This will give you better logging about what went wrong, because it uses the actual NSError parameter that is passed in.
You could also, depending on the format of the returned object, try converting your responseData and displaying that. I’ve worked with REST services that return their error messages as a JSON dictionary in the responseData, so I can convert that and see what the server is responding with in case of error, but this depends on the server you are connecting to.