I have successfully implemented the ASIHTTPRequest framework to download a file from a URL and it saves in my Document Directory - YIPPIE!!!
Now I would like to show a Progress of some sort, so that the user can what is going on.
With the UIWebViewDelegate there are the following methods - I was hoping to use something similar.
-(void)webViewDidStartLoad:(UIWebView *)webView
-(void)webViewDidFinishLoad:(UIWebView *)webView
Is there something similar to use with ASIHTTP??
Here is an example mixing MBProgressHUD and ASI.
The idea is you start your updating before the begin the request and you end the updating in either the completion or failed blocks.
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = #"Updating…";
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSError *error = [request error];
}];
[request startAsynchronous];
Related
i have tried to send username and password along with person image to server using ASIHTTPRequest and i have written some code but it's not working data is not sent to server please some body help me with example code
-(void)service_Call{
NSString *urlStr = [NSString URLWithString:urlstring];
NSLog(#"urlStr --->> %#",urlStr);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
[request setRequestMethod:#"POST"];
[request setPostValue:self.FnameTxt.text forKey:#"username"];
[request setPostValue:self.LnameTxt.text forKey:#"password"];
NSString *fileName = #"iphone.jpg";
// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
[request setData:imageData withFileName:fileName andContentType:#"image/jpeg" forKey:#"image"];
[request setDelegate:self];
[request startAsynchronous];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(#"response: %#", responseString);
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
But data is not sending to server please help me somebody if there is any error i code
You should use AFNetworking instead of ASIHTTPRequest, and you will find how handy it can be used.
Also, normally imageData should be sent as base64 string, if you send image by NSData, how your server side handle it?
Its my code , can you help where am I missing to handle Memory? I'm using X-Code 4.6. And also I have checked instrument , to get other memory leaks. Its almost showing all "ASIHTTPRequest". Im not handling manually like [request release]; Is that necessary to fix memory leak? Thanks in advance
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
}];
[request setFailedBlock:^{
}];
[request setTimeOutSeconds:60];
[request startAsynchronous];
It leaks because of the retain cycle created between the block and the request object.
Try the below:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
__weak ASIHTTPRequest *weakRequest = request;
[request setCompletionBlock:^{
}];
[request setFailedBlock:^{
}];
[request setTimeOutSeconds:60];
[request startAsynchronous];
Cheers..
EDIT:
ASIHTTPRequest is not supported anymore, try to move to AFNetworking, it is the best.
Then instead do this:
__weak __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
__strong ASIHTTPRequest *requestInBlock = request;
//your code, and replace all refrences of request inside the block with requestInBlock.
}];
[request setFailedBlock:^{
//same here
}];
[request setTimeOutSeconds:60];
[request startAsynchronous];
try this out.
I use AFNetworking + SVProgressHUD post to request data. SVProgressHUD will always display if the Network has a problem or very slow and internet links have been cut.
How AFNetworking to judge the network problem? Or request timeout?
I'm not sure what the implementation is for SVProgressHUD but I'm using a similar one called MBProgressHUD in my current application. Hopefully this advice still applies.
If I understand your question correctly, you're wondering how to dismiss your progressHUD if the AFNetworking call fails? If that's your question, then all you need to do is dismiss your HUD in your failure block as shown below.
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[navigationController.view addSubview:HUD];
HUD.dimBackground = YES;
[HUD show:YES];
//Set up the request
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"example.com"]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:#"POST"];
//Set up the operation
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Hide activity indicator after success
[HUD show:NO];
[HUD hide:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Hide activity indicator after failure
[HUD show:NO];
[HUD hide:YES];
}];
// Fire off the operation
[operation start];
My request is
NSURL *requestUrl = [NSURL URLWithString:url];
if(request_) {
[request_ setDelegate:nil];
[request_ release];
}
request_ = [[ASIFormDataRequest requestWithURL:requestUrl] retain];
[request_ setDelegate:self];
[request_ setRequestMethod:#"GET"];
[request_ setTimeOutSeconds:HTTP_TIME_OUT];
[request_ startAsynchronous];
The call is going since the network access indicator on the top bar is rotating and disappearing after a few seconds. But either of the delegate methods
- (void)requestFinished:(ASIHTTPRequest *)request
or
- (void)requestFailed:(ASIHTTPRequest *)request
is not being called. What could have went wrong?
Thanks in advance.
PS: Sometimes, due to some issue, server is returning a "500 Internal Server Error". But even in that case, shouldn't the delegate
- (void)requestFailed:(ASIHTTPRequest *)request
be called?
you can use the blocks to check that out.
here is the example of the same:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
}];
[request startAsynchronous];
I hope this might help.
Make sure your class is conforming to the ASIHTTPRequestDelegate protocol.
#interface YourClass : ... <ASIHTTPRequestDelegate>
OK. I'm not completely clear about blocks, but I do use them often; especially when doing an ASIHTTPRequest. I'd like to pass an object into the block and have the request assign a value to the object on completion, but I don't know how to make the object 'available' inside a block.
Here's my method...
- (void)fetchImageAsynchronously:(NSURL *)theURL intoImageObject:(UIImage *)anImageObject
{
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:theURL];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCompletionBlock:^{
NSData *responseData = [request responseData];
anImageObject = [UIImage imageWithData:responseData];
}];
[request setFailedBlock:^{
// NSError *error = [request error];
}];
[request startAsynchronous];
}
So, when the request completes, I want the value of anImageObject to be the fetched image. But anImageObject is not available inside the block.
Would someone kindly help?
anImageObject would have to be passed by reference. That is, UIImage**, and pass the address of anImageObject when calling the method.
This isn't a great design because you'll also have to manage the lifetime of anImageObject and also likely post some sort of notification that it is ready. That is, this code will break if anImageObject is deallocated in the time that it takes to download the image data. And you wont know that anImageObject was initialized with data or not.
- (void)fetchImageAsynchronously:(NSURL *)theURL intoImageObject:(UIImage **)anImageObject
{
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:theURL];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCompletionBlock:^{
NSData *responseData = [request responseData];
*anImageObject = [UIImage imageWithData:responseData];
}];
[request setFailedBlock:^{
// NSError *error = [request error];
}];
[request startAsynchronous];
}