Friday 26 August 2011

Sending Request to WebServer from iPhone App

Most of the apps have the communication with servers as the request is send from app and the response is received from server.
This blog describes how to send request on web servers and how to retrieve response of that request.


First we have to download ASIHTTPREQUEST package and copy it in our project.

You can download it from https://github.com/pokeb/asi-http-request/tree



Now you have to add the following frameworks in your frameworks folder:-

CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics and zlib

click on framework -add- add existing framework





like this add rest of the frameworks.




In your ViewController.h file import these classes

#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "ASIFormDataRequest.h"

//Create an instance of ASINetworkQueue
ASINetworkQueue *networkQueue;

you do not need to synthesize it.

-(void)getRegistrationResponseFailed:(ASIFormDataRequest *)req;
-(void)getRegistrationResponse:(ASIFormDataRequest *)req;
-(void)companyResponse:(ASIHTTPRequest *)req;
-(void)companyResponseFailed:(ASIHTTPRequest *)req;


In your ViewController.m file write code below:-

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

In your ViewDidLoad method write:-

networkQueue=[[ASINetworkQueue queue]retain];


Write the following code where you have to send request:-

There are various methods of sending request to server

The method below is POST Method

//create the url where the reqest will be send

NSString *str = [NSString stringWithFormat:@"%@/register.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

//setPostValue contains the value that you have to send if needed and forKey contains the key (means which script if you are calling in that the key name for the value parameter).

[request setPostValue:userMailTextField.text forKey:@"email"];
[request setPostValue:nicknameTextField.text forKey:@"nickName"];
[request setPostValue:passwordTextField.text forKey:@"password"];

[request setRequestMethod:@"POST"];
[request setDelegate:self];
//getRegistrationResponse and getRegistrationResponseFailed are the methods that will be called when either request will successfully execute or will fail.

[request setDidFinishSelector:@selector(getRegistrationResponse:)];
[request setDidFailSelector:@selector(getRegistrationResponseFailed:)];
[networkQueue addOperation: request];
[networkQueue go];



-(void)getRegistrationResponseFailed:(ASIFormDataRequest *)req
{
[indicator stopAnimating];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not get a response from the server.Please try again after some time" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}


-(void)getRegistrationResponse:(ASIFormDataRequest *)req
{
[indicator stopAnimating];
NSData *response=[req responseData];
NSString *string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"congrats" message:@"request send successfully" time" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}


GET Method of sending request:-


NSString *str = [NSString stringWithFormat:@"http://neptuneapps.com/bizproxapp/phpscripts /getBusinesses.php?categoryid=%@",mainbusinessID];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setRequestMethod:@"GET"];
[request setDelegate:self];
[request setDidFinishSelector: @selector(companyResponse:)];
[request setDidFailSelector: @selector(companyResponseFailed:)];
[networkQueue addOperation: request];
[networkQueue go];

-(void)companyResponseFailed:(ASIHTTPRequest *)req
{
[indicator stopAnimating];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not get a response from the server.Please try again after some time" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}


-(void)companyResponse:(ASIHTTPRequest *)req
{
[indicator stopAnimating];
NSData *response=[req responseData];
NSString *string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"congrats" message:@"request send successfully" time" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

}


Things to remember:-

in dealloc mehod write:-

- (void)dealloc {
[networkQueue cancelAllOperations];
[networkQueue release];

[super dealloc];
}

in viewWillDisappear write :-

-(void)viewWillDisappear:(BOOL)animated
{
[networkQueue cancelAllOperations];

}

No comments:

Post a Comment