How to post data through ASIHTTPRequest in iphone - iphone

How to post form field data (name, email,phonenumber,score) through ASIHTTPRequest. I am using json webservices.

Try this , assign variable in your header class
ASIFormDataRequest *requestASI;
in your .m file
-(void)startASIRequest {
NSString *strUrl = [NSString stringWithFormat:#"http://your url"];
NSURL *url = [NSURL URLWithString:strUrl];
[self setRequestASI:[ASIFormDataRequest requestWithURL:url]];
[requestASI setDelegate:self];
[requestASI setRequestMethod:#"POST"];
[requestASI setPostValue:#"1" forKey:#"name"];
[requestASI setDidFailSelector:#selector(uploadFailed:)];
[requestASI setDidFinishSelector:#selector(uploadFinished:)];
[requestASI startAsynchronous];
- (void)uploadFinished:(ASIHTTPRequest *)theRequest {
NSLog(#"registerFinished %#",[theRequest responseString]);
}
- (void)uploadFailed:(ASIHTTPRequest *)theRequest {
NSLog(#"registerFailed %#", [theRequest error]);
}

Related

Google Api Using With Json

When I Call Google API for get the address and i calling api here like .
-(void)searchviews:(NSString*)EditString selector:(SEL)sel
{
NSLog(#"Welcome To Search views");
searchviews=sel;
NSString *path =[NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?address=%#&sensor=false",EditString];
NSURL *url=[NSURL URLWithString:path];
NSLog(#"hiii---%#",url);
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startAsynchronous];
[drkSignUp showWithMessage:nil];
NSLog(#" Complet--------------- ");
and for Request Method I call like .
- (void)requestFinished:(ASIHTTPRequest *)request {
//NSLog(#"%#",[request responseString]);
NSString *func = [self getFunc:[request url]];
NSLog(#"%#\n%#",func,[request responseString]);
if ([func isEqual:#"json?address=%#&sensor=false"])
{
NSDictionary *resDict = [parser objectWithString:[request responseString] error:nil];
NSLog(#"---- ResData%#",resDict);
NSString *result = [resDict objectForKey:#"successful"];
NSLog(#"hiiiii google api calling............");
[drkSignUp hide];
[self.delegate performSelector:searchviews withObject:[resDict objectForKey:#"results"]];
the is like that but problem create in fun . When i call
if ([func isEqual:#"json?address=%#&sensor=false"])
it is not calling cos the it is be Dynamic String.So What Should I put inplace of %# in func ?
You can fill the userInfo dictionary of ASIFormDataRequest like the following
//After this line
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
//Add
request.userInfo = [NSDictionary dictionaryWithObject:#"EditString" forKey:#"request"];
Then in
- (void)requestFinished:(ASIHTTPRequest *)request {
//Get the request userInfo
NSString *str = [request.userInfo objectForKey:#"request"];
//now fill the request string
NSString *requestString = [NSString stringWithFormat:#"json?address=%#&sensor=false", str];
//Check it with func
if ([func isEqual:requestString])
{
//Continue your procedure
}
}

ASIHTTP Request

Please any one know abhout how to send request and send a file in ASIHTTP with some parameters my code is bellow i am not able to send file
NSURL *audiourl = [NSURL URLWithString:#"http://dev.kuwait.tt/demo_radioapps/radio_mobile/adduserrequest.php?program_id=3&user_name=abhishek&user_email_id=abhi#tets.com&audiofile=review.caf&mode=AddRequest"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
[request addData:tempData withFileName:#"review.caf" andContentType:#"audio/caf" forKey:#"audiofile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
have you tried: [request setData:tempData withFileName:#"review.caf" andContentType:#"audio/caf" forKey:#"audiofile"];
// Add the contents of an NSData object to the request and you can specify the content-type and file name
- (void)addData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key
// Add the contents of an NSData object to the request, clearing any others with the same key
and you can specify the content-type and file name
- (void)setData:(id)data withFileName:(NSString *)fileName andContentType:(NSString *)contentType forKey:(NSString *)key;
perhaps you can validate the success of the request with
if ([request responseStatusCode] == 200) {
NSLog (#"Response status code: %i", [request responseStatusCode]); }
Below is a working function which i have used in my application , please check if it helps:
- (void)uploadAudioFile:(NSString *)audioPath{
NSURL *audiourl=[NSURL URLWithString:audioPath];
NSData *audioData=[NSData dataWithContentsOfFile:audiourl.path];
#try {
NSString *strURL = #"http://www.abc.com/data/default.aspx";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
request= [request initWithURL:[NSURL URLWithString:strURL]];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setShouldAttemptPersistentConnection:YES];
[request setPostValue:[Settings UserName] forKey:#"lblUsername"];
[request setPostValue:[Settings Password] forKey:#"lblpassword"];
[request addData:audioData withFileName:#"audio.caf" andContentType:#"audio/x-caf" forKey:#"filMyFile"];
[request setCompletionBlock:^{
NSLog(#"Request Completed");
[self requestFinished:request];
}];
//if request failed
[request setFailedBlock:^{
[self requestFailed:request];
NSLog(#"request Failed: %#",[request error]);
}];
[request startAsynchronous];
}
#catch (NSException *exception) {
NSLog(#"Exception name and reson is %# ------- %#",exception.name, exception.reason);
}
#finally {
NSLog(#"finalyy of upload audio");
}
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(#"requestFinished");
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSLog(#"requestFailed");
}

ASIHTTPRequest appendPostData not working

I want to send String Data to a php server using the ASIHTTPRequest appendPostData method but its not working.
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[#"123456" dataUsingEncoding:NSUTF8StringEncoding]];
[request startAsynchronous];
I tried some modifications on the request itself like:
[request setRequestMethod:#"POST"];
[request buildPostBody];
but also this does not work.
When i use the
[request addPostValue:#"Ben" forKey:#"names"];
syntax it does work.
Anybody has an idea whats wrong here?!
I usually use this:
- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {
//localCopy = self;
self.delegate = requestDelegate;
self.callback = requestSelector;
self.errorCallback = errorSelector;
NSURL *url = [NSURL URLWithString:string];
postRequest = [[ASIFormDataRequest alloc] initWithURL:url];
[postRequest setDelegate:self];
[postRequest setRequestMethod:#"POST"];
if (stringDictionary)
for (NSString *key in [stringDictionary allKeys])
[postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key];
if (dataDictionary)
for (NSString *key in [dataDictionary allKeys])
[postRequest setData:[dataDictionary objectForKey:key] forKey:key];
//NSLog(#"request url = %#", [postRequest.url absoluteString]);
[postRequest startAsynchronous];
}
Parameters:
(NSString *)string - url string, where to post your request;
(NSDictionary *)stringDictionary - dictionary, which contains all the text information (such as name, id etc.);
(NSDictionary *)dataDictionary - dictionary, which contains all data information (such as photos, files, etc.);
(id)requestDelegate - delegate to perform selectors below;
(SEL)requestSelector - selector, which will be executed while successfully request;
(SEL)errorSelector - selector, which will be executed, while error occurred.
P.S. Selectors will be used in the ASIHTTPRequest Delegate implementation

Iphone ASIHTTP setDidFinishSelector issue

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];
}

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.