Iphone ASIHTTP setDidFinishSelector issue - iphone

i have the following code:
- (void)imageDownloaded:(ASIHTTPRequest *) request idDisco:(NSNumber *)iddisco
NSLog(#"%d",[idPlace intValue]);
}
And
NSURL *url = [NSURL URLWithString:[item objectForKey:#"image"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:[self performSelector:#selector(imageDownloaded:idDisco:)withObject:request withObject:disco.id_disco]];
But the compiler tells me that "passing argument 1 of setDidFinishSelector from incompatible pointe type"
It works fine but i don't know what i'm doing wrong.
Thanks

You have to pass #selector(someMethod:) there:
And the signature of the method should be
- (void)methodName:(ASIHTTPRequest *)request;
Look at this example:
- (IBAction)grabURLInTheBackground:(id)sender
{
if (![self queue]) {
[self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
}
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[[self queue] addOperation:request]; //queue is an NSOperationQueue
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
}
- (void)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}

Related

HTTP Request with nested dictionary

I need to access a web service with a POST method, with nested dictionary to receive relevant data. But I keep getting empty response. What am I doing wrong?
NSURL *url = [NSURL URLWithString:#"http://appzander.com/gateway.php"];
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *request = _request;
AppDelegate *app = [[UIApplication sharedApplication] delegate];
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:app.myLocation.longitude],#"longitude",
[NSNumber numberWithDouble:app.myLocation.latitude],#"latitude",
#"500000",#"radius",
#"1000",#"maxResults",
nil];
NSDictionary *requestDic = [[NSDictionary alloc] initWithObjectsAndKeys:
#"GetNearestStations",#"function",
#"false",#"debug",
paramsDic,#"params",
nil];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request appendPostData:[[SBJsonWriter new] dataWithObject:requestDic]];
request.requestMethod = #"POST";
[request setDelegate:self];
[request setCompletionBlock:^{
NSData *responseString = [request responseData];// responseString];
NSLog(#"Response: %#", responseString);
[self plotCrimePositions:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
// 6
[request startAsynchronous];
Use ASIFormDataRequest
NSURL *url = [NSURL URLWithString:#"yourUrlhere"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:email forKey:#"username"];
[request setPostValue:pasw forKey:#"pass"];
[request setDelegate:self];
[request startAsynchronous];
Solved- I wasn't sending the parameters properly:
NSURL *url = [NSURL URLWithString:#"http://appzander.com/gateway.php"];
ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
__weak ASIFormDataRequest *request = _request;
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithDouble:32.071738],#"longitude",
[NSNumber numberWithDouble:34.791295],#"langitude",
#"500000",#"radius",
#"1000",#"maxResults",
nil];
[request setPostValue:#"false" forKey:#"debug"];
[request setPostValue:#"GetNearestStations" forKey:#"function"];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *paramsDicJSON = [jsonWriter stringWithObject:paramsDic];
[request setPostValue:paramsDicJSON forKey:#"params"];
request.requestMethod = #"POST";
[request setDelegate:self];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
[self plotBikes:responseString];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request startAsynchronous];

Getting an ARC warning for capturing an ojbect strongly

I'm using ARC and getting a warning saying Capturing 'request' strongly in this block is likely to a retain cycle.
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"%#", error.description);
}];
I'm assuming request is declared somewhere before the blocks. You need to declare it as __weak, or set a second, weakly-declared variable to it.
This question is similar. Try this:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
__weak ASIHTTPRequest *request_b = request;
[request setCompletionBlock:^{
NSString *responseString = [request_b responseString];
self.appointmentArray = [responseString JSONValue];
}];
[request setFailedBlock:^{
NSError *error = [request_b error];
NSLog(#"%#", error.description);
}];
Simply replacing:
__block ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
with:
__weak ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
will suffice.

Problem with Response asihtttprequest

NSURL *url = [NSURL URLWithString:#"someurl"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:year1 forKey:#"year"];
[request setPostValue:appy_level forKey:#"appy_level"];
[request setPostValue:reasons forKey:#"reasons"];
[request setPostValue:country forKey:#"country"];
[request setPostValue:city forKey:#"city"];
[request setPostValue:sex forKey:#"sex"];
[request setRequestMethod:#"POST"];
[request setValidatesSecureCertificate:NO];
[request setDelegate:self];
[request startAsynchronous];
NSLog(#"response -%#",[request responseString]);
[self dismissModalViewControllerAnimated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
NSLog(#"%#",response);
}
any idea why this code return (null) i mean response is null? and this requestFinised: method isnt working even i write[requestsetDidFinishSelector:#selector(requestFinished)];
method too.I am confuesed right now.
The first NSLog won't work because you start your request as asynchronous.
You chould try putting NSLog(#"finished"); in -requestFinished: to check if page is loading or not. It can be an error in your server file (e.g. php fatal error).
Have you tried this:
NSString *response = [[NSString alloc] initWithData:[request data] encoding:NSUTF8StringEncoding];
NSLog(#"%#", response);

ASIHTTPRequest - HTTPS

Does ASIHTTPRequest support HTTPS connections? My connection right now works for a HTTP connection and errors if I try a HTTPS Connection. (Goes into requestFailed and gives me a ASIHTTPErrorRequestDomain)
-(void) getData
{
av.hidden = NO;
[av startAnimating];
NSString *urlString = [IP stringByAppendingString:#"Method1"];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSLog(#"URL = %#",url);
[request setRequestMethod:#"POST"];
[request setPostValue:#"val1" forKey:#"key1"];
[request setPostValue:#"val2" forKey:#"key2"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
//NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
[self parseData:responseData];
[av stopAnimating];
av.hidden = YES;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
[av stopAnimating];
av.hidden = YES;
}
Thanks,
Teja
Whoops, sorry, figured it out -
[request setValidatesSecureCertificate:NO] works for reference.
Thanks to these guys - http://www.iphonedevsdk.com/forum/iphone-sdk-development/29417-asihttprequest-library-works-https.html
EDIT: Since this is getting some upvotes, I'd just like to add that this might not be the best approach for valid SSL certificates. The one I was using was a self-signed certificate, so this was fine.

Is the ASIHTTPRequest is not asynchronous?

I am using ASIHTTPRequest to fetch some data from a web service.
I am making requests using a loop.
The problem is that it doesn't seem the request is going asynchronously so that my activityindicator is not working .
Is it true that ASIHTTPRequest is not asynchronous .
or should i use the regular nsmutablerequest to perform asynchronous request .
You should put your request in a download queue, i.e.
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:request];
[request release];
Just
[request startAsynchronous];
runs the request on the UI thread, so you should try it with download queue.
For Sync Synchronous
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
Creating an asynchronous request
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
And there is lot of more options like Queue and many more
You can refer http://allseeing-i.com/ASIHTTPRequest/How-to-use