ASIDownLoadCache doesn't work - iphone

ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:url]];
request.requestHeaders = header;
request.requestMethod = #"GET";
request.tag = DBRequestTypeChannelCategory;
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:2];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setSecondsToCache:60*60*24*3];
[request startAsynchronous];
this is my code about http
and if i turn my phone to fly model.
i got this
Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x1fd67bf0 {NSUnderlyingError=0x1fd66bf0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 2.)", NSLocalizedDescription=A connection failure occurred}

The error you've posted means that the crash was from a connection failure, which makes sense if you have airplane mode enabled. You should be able to eliminate this be setting a failure handler.
[request setFailedBlock:void^{
//
}];
Or
[request setDidFailSelector:#selector(requestWentWrong:)];
To access cached data without internet access simply add the following to your request.
[request setCachePolicy:ASIFallbackToCacheIfLoadFailsCachePolicy];
In order for this to work you need to make sure that cache storage is set to permanent to prevent the caches from being removed when the user exists the app.
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

Related

how to compress and upload High qualilty video to the server

uploading HDvideo to the server using ASIFormDataRequest.
but it taking long time to upload.
my code is
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
if (movieUrl != Nil) {
NSData *videoData = [NSData dataWithContentsOfURL:movieUrl];
[request addData:videoData withFileName:[movieUrl lastPathComponent] andContentType:#"audio/mp4" forKey:#"video"];
[request setRequestMethod:#"POST"];
//
[request setTimeOutSeconds:600];
[request setDelegate:self];
//
[request setUploadProgressDelegate:progressView];
[request startSynchronous];
where movieurl is url return from imagepickerdidfinish
movieUrl = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
Well you are doing All of this synchronously which means your interface is blocked and seem unresponsive. I would recommend using asynchronous connection which will be way more responsive, executing on a separate thread and not blocking the main thread. Use:
[request startAsynchronous]

Log into website with ASIHttpRequest

I have a website that i want to log into from my iphone in a iphone app i am making, i followed the documentation on ASIHttpRequests website but all i get from the response string is the html code for the login page , but i get a OK http code, Why is this happening?
Here is my code :
-(IBAction)fetchData:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.rssit.site90.com/login.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request shouldPresentCredentialsBeforeChallenge];
[request setRequestMethod:#"POST"];
[request setPostValue:#"" forKey:#"username"];
[request setPostValue:#"" forKey:#"password"];
[request setDelegate:self];
[request startAsynchronous];
//Add finish or failed selector
[request setDidFinishSelector:#selector(requestLoginFinished:)];
[request setDidFailSelector:#selector(requestLoginFailed:)];
}
- (void)requestLoginFinished:(ASIHTTPRequest *)request
{
NSString *yourResponse = [request responseString];
NSLog(#"%#", yourResponse);
}
The form on that page is doing a get, not a post. Your server is probably not expecting POST data, and is looking at query string params instead. Change to
[NSURL URLWithString:#"http://www.rssit.site90.com/login.php?username=YOURUSERNAME&password=YOURPASSWORD"];
and set the requestMethod to GET.

How do I use ASI Http in iOS to POST data to a web service?

I am using the instructions provided in the ASI page here. I am trying to send some data to a web service and not seeing any results.
This is my sendRequest method which gets called in viewDidLoad
-(void)sendRequest {
NSURL *url = [NSURL URLWithString:#"http://153.60.6.75:8080/BarcodePayment/transactions"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
NSString *dataContent = #"{\"id\":7,\"amount\":7.0,\"paid\":true}";
NSLog(#"dataContent: %#", dataContent);
[request appendPostData:[dataContent dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
}
I check the dataContent string and the output is
{"id":7,"amount":7.0,"paid":true}
If I use curl from Terminal, I checked and this command works.
curl -X POST -H 'Accept:application/json' -H 'Content-Type: application/json' http://153.60.6.75:8080/BarcodePayment/transactions/ --data '{"id":7,"amount":7.0,"paid":true}'
My understanding is that in using curl, I set it to json, specify the address, specify the data which is equivalent to dataContent in my code. Nothing happens. What's wrong?
Thanks for the help!
You have pretty much everything except the most crucial component which is to start the request
You need to add [request startSynchronous]; for a Synchronous Request or [requester startAsynchronous]; for a Asynchronous request (and you possibly need the Delegate Methods to handle any response you have back)
This is all covered pretty nicely in the ASIHTTPRequest How to Use Guide. The Sections most relevant to this would be 'Creating a synchronous request' and 'Creating an asynchronous request'. Also something to think about taken from that page:
In general, you should use asynchronous requests in preference to synchronous requests. When you use ASIHTTPRequest synchronously from the main thread, your application’s user interface will lock up and become unusable for the duration of the request.
You forgot to call:
[request setDelegate: self];
[request startAsynchronous];
Or:
[request startSynchronous];
If you don't call any of these, the request will never be made :)
I don't see a call to [request startSynchronous] (or [request startAsynchronous]) in your code... Are you even initiating the request anywhere?
It looks like you didn't call [request startAsynchronous];
Check ASIHTTPRequest documentation for more details on how to use
You're not actually sending the request.
1) You'll need to call [request startSynchronous] or [request startAsynchronous]
2) If you use asynchronous (which you should probably do) you'll need to set the delegate and implement a - (void)requestFinished:(ASIHTTPRequest *)request method.
- (void)postThresholdDetails:(NSDictionary *)info
{
NSString *urlString = [NSString stringWithFormat:#"%#%#/",BaseUrl,PostThresholdDetails];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:120];
[request addRequestHeader:#"Accept" value:#"application/json"];
[request addRequestHeader:#"content-type" value:#"application/x-www-form-urlencoded"];
request.allowCompressedResponse = NO;
request.useCookiePersistence = NO;
request.shouldCompressRequestBody = NO;
[request setPostBody:[NSMutableData dataWithData: [info objectForKey:#"jsondata"] ]];
[request startAsynchronous];
}

ASIFormDataRequest POST returning website source code?

I am using the following code to set the username and password to a form on a website:
ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setRequestMethod:#"POST"];
[request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:kUsername] forKey:#"username"];
[request setPostValue:[[NSUserDefaults standardUserDefaults] objectForKey:kPassword] forKey:#"password"];
[request setTimeOutSeconds:40];
[request setDelegate:self];
[request startAsynchronous];
However I am NSLogging the responseString from this request, and it is just printing out the source code of the website rather than any information.
This is likely a server-side issue or a logic error. In addition to responseString, look at responseHeaders and responseStatusCode to make sure you're getting what you expect.

asihttprequest: post problems on the iphone

I am trying to to establish a connection between my app and an internet service and I am using asihttprequest but I'm having a small problem. Everything works great when I am on WiFi but when I turn it off and use GPRS(EDGE) or 3G nothing seems to work. Should I change something.
Here is some of my code
[self setRequest:[ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"example.url.php"]]];
[request setPostValue:textString forKey:#"mytext"];
[request setData:imageData withFileName:theFinal andContentType:#"image/png" forKey:#"userfile"];
[request setPostValue:textString2 forKey:#"description"];
[request setPostValue:latitude forKey:#"latitude"];
[request setPostValue:longitude forKey:#"longitude"];
[request setPostValue:finalIdString forKey:#"city_id"];
[request setTimeOutSeconds:60];
[request setUploadProgressDelegate:progressIndicator];
[request setDelegate:self];
[request setDidFailSelector:#selector(uploadFailed:)];
[request setDidFinishSelector:#selector(uploadFinished:)];
[request startAsynchronous];
found it i had to use
[ASIHTTPRequest setShouldThrottleBandwidthForWWAN];