How to tell when a request is done (ASIHTTPRequest)? - iphone

How do you tell when a request is finished (using ASIHTTPRequest)? Here is the code I am using:
[request setDidFinishSelector:#selector(requestFinished:)];
...
-(void)requestFinished:(ASIHTTPRequest *)request {
NSLog(#"The request is done.");
}
However, when request is done, "The request is done" is never printed to the log. Am I doing something wrong? Thanks.

maybe your connection is failing? In this case, you should test for - (void)requestFailed:(ASIHTTPRequest *)request
Did you set the request delegate? Example code:
- (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];
}

I forgot to set the delegate to self. Oops!
[request setDelegate:self];

Related

UIActivityIndicator

When I start UIActivityIndicatorView using StartAnimating method:
[ActivityIcon startAnimating];
it disable all user interactions so when the user Tap on Cancel button which should abort the download process and hide the UIActivityIndicator it does not work!!!
any suggestions would be appreciated.
Edit:
I am using separate thread to download the files in the background. All progress reporting and UI interaction I made it through:
[self performSelectorOnMainThread:#selector(RefreshScreen:) withObject:nil waitUntilDone:YES];
and RefreshScreen method is the one who interact with the UI elements.
try change this line: [request startSynchronous]; to: [request startAsynchronous];
EDIT
- (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];
}

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");
}

Upload video taken with iphone camera with PUT method

How can i upload a video taken with the camera or contained in the phone memory?
I need to upload it using a PUT method.
Thank you
Code for photo uploading:
- (void)uploadPhoto:(NSData *)data
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"PUT"];
[request addRequestHeader:#"Date" value:timeStamp];
[request addRequestHeader:#"Auth" value:signature];
[request addRequestHeader:#"Client" value:apiClient];
[request addRequestHeader:#"Accept" value:apiAccept];
[request addRequestHeader:#"USER" value:userHeader];
[request appendPostData:data];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(#"%#",responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
NSLog(#"%#",responseData);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(#"%#",error);
}
none of the 3 methods at the end are called after i make this request.
Please help me.
Regards,
response string:
|?xml version="1.0" encoding="UTF-8"?|
|error|
|code|InvalidParameter|/code|
|message|Invalid parameter specified (user)|/message|
|/error|
You can do it using ASIHTTPRequest. Here is a sample:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:myVideoData];
[request setRequestMethod:#"PUT"];

How to read a file from a website using objective-c & xcode

I am trying to write a code that will grab some information provided by my server (remote), for example request a url that will return data that will be represented in the application after parsing it.
I've been trying since 2 days now I googled it i found some incomplete solution but nothing really worked out for me
I am really noob in Xcode and Objective-C
Thanks
Click for the URL-Loading documentation provided by Apple.
Especially Using NSURLConnection looks interesting for you.
Edit:
Another very good and easy-to-use Framework for this task is ASIHTTP:
Click
The easiest way:
- (void)grabURL
{
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];
}
}
Asynchronous loading is only slightly more complex:
- (void)grabURLInBackground
{
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];
}

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.