Is the ASIHTTPRequest is not asynchronous? - iphone

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

Related

How to show UIProgressView with ASIFormDataRequest

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

Post Data but GET Method json -ios

how can i use the get method to post data on an api link with 6 values and a json format?
im really fuzzled. im doing this for days . is it best to have 6 textfields that will be used to write the value and a button to indicate post data?
check out my code and please help.
-(IBAction)postDataPressed
{
NSString *urlString = #"http://192.168.18.8/apisample2/friendb.php?fm=jsn";
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:user.text forKey:#"un"];
[request setPostValue:pass.text forKey:#"pd"];
[request setPostValue:gender.text forKey:#"gd"];
[request setPostValue:age.text forKey:#"ag"];
[request setPostValue:status.text forKey:#"st"];
[request setPostValue:lookfor.text forKey:#"lf"];
[request setRequestMethod:#"GET"];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSLog(#"Response: %#", responseString);
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(#"Error: %#", error.localizedDescription);
}];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request startAsynchronous];
}
i hope someone will help.
NSString *request_url = [NSString stringWithFormat: #"http://192.168.18.8/apisample2/friendb.php?fm=jsn&un=%#&pd=%#&gd=%#&ag=%#&st=%#&lf=%#",
user.text, pass.text, gender.text, age.text,status.text,lookfor.text];
NSURL *url = [NSURL URLWithString:request_url];
ASIHttpRequest *request = [ASIHttpRequest requestWithURL:url];
request.delegate = self;
....
HTTP Get method only accept data in url, called query string, so you have to build query string yourself.

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

Problem with Response asihtttprequest

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

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