I hope to access the instance of ASIHTTPRequest
{
ASIHTTPRequest *myRequest;
}
#property (nonatomic, retain) ASIHTTPRequest *myRequest;
//-----------------------------------
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:requestURL]];
[request setTag:selectTag];
myRequest=request;
[request startAsynchronous];
if I force the request stoping work
I used the codes
if (myRequest!=nil)
{
[myRequest cancel];
}
but myRequest always returns 0x00000000
Welcome any comment
I think that you need to test if the request is already finished or canceled.
if (myRequest && ![myRequest isCancelled] && ![_request isFinished])
{
[myRequest cancel];
myRequest = nil;
}
try this its work for me
NSURL *url = [NSURL URLWithString:#"Your Url"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:email forKey:#"email_id"];
[request setPostValue:pasw forKey:#"user_password"];
[request setDelegate:self];
[request startAsynchronous];
Related
I want to show uiprogress bar with ASIFormDataRequest. I googled it but i can't find regarding uiprogressView with ASIFormDataRequest, I just find uiprogressView with NSURLConnection. just let me know if it is possible.
// So this my method where i upload the data.
-(NSMutableArray*)getUploadVideo:(NSString*)titleName userID:(NSString*)userId token:(NSString*)tokenID description:(NSString*)desc videoID:(NSString*)video_id privateOrPublic:(NSString*)privacyID pathOfVideoFile:(NSString*)path mode:(NSString*)potLand;
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://%#/aircastdev/RestServices/fileUpload"],kId];
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
[request setDidFinishSelector:#selector(requestFailed:)];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setPostValue:titleName forKey:#"title"];
[request setPostValue:userId forKey:#"user_id"];
[request setPostValue:tokenID forKey:#"token"];
[request setPostValue:desc forKey:#"desc"];
[request setPostValue:video_id forKey:#"video_id"];
[request setPostValue:privacyID forKey:#"is_private"];
[request setPostValue:potLand forKey:#"mode"];
NSString *url = [[NSURL fileURLWithPath:path] path];
[request setFile:url withFileName:[NSString stringWithFormat:#"%#.mov",titleName] andContentType:#"video/MOV" forKey:#"file"];
[request setTimeOutSeconds:50000];
[request setRequestMethod:#"POST"];
// EDITED LINE
[request setUploadProgressDelegate:myProgressView];
[request startSynchronous];
SBJSON *sbJason = [[SBJSON alloc] init];
getUploadArray = [sbJason objectWithString:self.response];
return getUploadArray;
}
// when upload is done this method call.
- (void)requestFinished:(ASIHTTPRequest *)request
{
// alert: request is successfully sent
self.response = [request responseString]; //stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
use this:
[request setDownloadProgressDelegate:progressView];
or
[request setUploadProgressDelegate:myProgressIndicator];
Where progressView is a UIProgressView
also have a look here
Here I am sending the file path or file with some parameters. But the server didn't get the video. So any problem in that code? Or anything you want to add something. Then please tell me. Any delegate method you want to tell me where I get error or setting in response .
NSString *path = [[NSBundle mainBundle] pathForResource:#"hangover" ofType:#"mp4"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:#"name"];
[request setPostValue:#"GUnit" forKey:#"title"];
[request setPostValue:#"133" forKey:#"user_id"];
[request setPostValue:#"8953d0e1c97ef83c9f0aff47" forKey:#"token"];
[request setPostValue:#"video song" forKey:#"desc"];
[request setPostValue:#"34" forKey:#"video_id"];
[request setPostValue:#"0" forKey:#"is_private"];
[request setFile:path forKey:#"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
Try this...
NSString *path = [[NSBundle mainBundle] pathForResource:#"hangover" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSData *movieData = [NSData dataWithContentsOfURL:fileURL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"http://156.75.28.172:52/aircas/RestServices/fileUpload"]];
[request setPostValue:filename forKey:#"name"];
[request setPostValue:#"GUnit" forKey:#"title"];
[request setPostValue:#"133" forKey:#"user_id"];
[request setPostValue:#"8953d0e1c97ef83c9f0aff47" forKey:#"token"];
[request setPostValue:#"video song" forKey:#"desc"];
[request setPostValue:#"34" forKey:#"video_id"];
[request setPostValue:#"0" forKey:#"is_private"];
[self.request setData:movieData withFileName:#"hangover.mp4" andContentType:#"multipart/form-data" forKey:#"video"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
Hope it helps.
Add following code in your code and than you can write 2 delegate methods and can handle response.
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
- (void) uploadFailed:(ASIHTTPRequest *)request
{
//Upload succeeded;
}
- (void) uploadFinished:(ASIHTTPRequest *)request
{
//Upload failed;
NSError *error = [request error];
NSLog(#"Error Description-->%#",[error localizedDescription]);
}
To have indepth view for ASIHttpRequest please see documentation. Its very well documented.
I am using ASIHTTPRequest 1.6.2 lib for all http transactions in IPhone. But i dont know, how can i post the data with ASIHTTPRequest in iPhone?
Can you please give me the code snippet which will work in in iphone?
I am using the following code for this. But i am getting response code as 0. Please help me to understand where i am going wrong.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:inputXml forKey:#"inputXml"];
[request setPostValue:#"qftS6TJN343343V84hw=" forKey:#"key"];
[request setPostValue:#"1.2" forKey:#"version"];
[request setRequestMethod:#"POST"];
[request startAsynchronous];
You should use ASIFormDataRequest:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
[request setPostValue:#"Copsey" forKey:#"last_name"];
[request setFile:#"/Users/ben/Desktop/ben.jpg" forKey:#"photo"];
Here's the how to page. It covers all types of requests such as get, form post, custom post, put, etc...
http://allseeing-i.com/ASIHTTPRequest/How-to-use
Here's how to set it up in your XCode project:
http://allseeing-i.com/ASIHTTPRequest/Setup-instructions
Those instructions and snippets should work in iOS.
Following is the sample code...
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request appendPostData:[reqString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:60];
[request startAsynchronous];
You will get detail guidance from here.
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:]];
NSMutableDictionary *dicData = [[NSMutableDictionary alloc] initWithCapacity:1];
[dicData setObject:txtPassword.text forKey:#"Password"];
[dicData setObject:txtEmailID.text forKey:#"Email"];
[request setPostBody:[[[self parseJsonFromObject:dicData] dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]];
[request setRequestHeaders:[NSMutableDictionary dictionaryWithObjectsAndKeys:#"application/json", #"Content-Type", nil]];
[request setDelegate:self];
[request setDidFinishSelector:#selector(signUpWithEmailFinish:)];
[request setDidFailSelector:#selector(signUpWithEmailFail:)];
[request startAsynchronous];
(void)signUpWithEmailFinish:(ASIHTTPRequest *)request
{
if (request.responseStatusCode == 200)
{
NSDictionary *responseMessage = [self objectFromJson:request.responseString];
NSLog(#"ResponseMEssage=%#",responseMessage);
if (responseMessage)
{
if ([responseMessage objectForKey:#"user"] == nil)
{
NSLog(#"duplication not allowed");
[self animation:0];
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_LOGIN_SUCCESS object:nil];
NSLog(#"Registration Complete");
UserLoginPage *userLogin=[[UserLoginPage alloc]init];
[self.navigationController pushViewController:userLogin animated:YES];
return;
}
}
}
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);
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