Occasionally the programmer needs to communicate with the server to send and receive data from the iPhone. Usually the communication is done to get data from the database that is in the server. To communicate with the server the programmer has to use Application Programming Interface(API). The data is sent through ASIHTTPRequest to the required URL and the server responds with the appropriate response data, which can be used for further processing in the app.
The steps to hit an API and get the response data are as follows.
-(void)methodName
{
ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://www.theWebSite.com"]];
[request1 setDelegate:self];
[request1 setDidFinishSelector:@selector(requestFinished:)];
[request1 setDidFailSelector:@selector(requestFailed:)];
[request1 startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSLog(@"response data %@ ", responseData);
// OR
NSString *responseString = [request responseString];
NSlog(@"response string is %@", responseString);
}
-(void)requestFailed:(ASIHTTPRequest *)error
{
NSLog(@"request failed");
NSLog(@"error %@", [[error error] localizedDescription]);
}
0 Comment(s)