Synchronous ASIHTTPRequest updating label - iphone

It seems very simple but not easy for me.. I am calling a few Synchronous ASIHTTPRequests and when each request is finished, I want to update a Label like following..
self.status.text = #"Google";
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
self.status.text = #"Yahoo";
NSURL *url = [NSURL URLWithString:#"http://www.yahoo.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
self.status.text = #"Apple";
NSURL *url = [NSURL URLWithString:#"http://www.Apple.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
However, it only show nothing but "Apple" once all calls are done.. What is the simple and best way to achieve this?

UIKit has no chance to redraw the label because you're blocking the main thread for the whole url request.
You should use asynchronous requests instead, so the UI will be responsive all the time. Also update the UI in a completion block:
self.status.text = #"Google";
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
self.status.text = #"Yahoo";
// start another request etc.
}];
[request startAsynchronous];

Related

access an instance of ASIHTTP

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

how to send post request with nsurlconnection

I have changed over to NSURLConnection and NSURLRequest delegates for my db connections etc.
I was using the ASIHTTPRequest libraries to do all this stuff but finally decided to move on due to the lack of support for that 3rd party library.
what I am woundering is how do I send post requests to my db like you do with the ASIFormDataRequest as shown below
//This sets up all other request
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setValidatesSecureCertificate:NO];
[request setPostValue:#"ClientDataSet.xml" forKey:#"filename"];
[request startSynchronous];
i.e. how to do send the setPostValues with the NSURLRequest class?
any help would be greatly appreciated
Using NSURLRequest is slightly more difficult than utilizing ASIHTTPRequest. You have to build your own post body.
NSData *postBodyData = [NSData dataWithBytes: [postBodyString UTF8String] length:[postBodyString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourURL];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postBodyData];

Problems with using POST from iPhone to REST service

I'm building an iPhone client that will GET and POST to a REST service.
The GET-part works fine, but I don't seem to be able to POST anything. The server only accepts xml at the moment.
I've tried to use ASIHTTPRequest, ASIFormDataRequest and doing it myself but nothing seems to work.
Can anyone help me? I'm fairly new to Objective-C programming.
I do get a status code: 200 as a response using the following code in didReceiveResponse:
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
int myStatus = httpResponse.statusCode;
if (myStatus == 400) {
NSLog(#"Status code: 400");
} else if (myStatus == 200) {
NSLog(#"Status code: 200");
} else {
NSLog(#"Other status code");
Here follows three different approaches...
Code (ASIHTTPRequest):
NSURL *url = [NSURL URLWithString:#"myUrl.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[#"<my xml>" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
Code (ASIFormDataRequest):
NSURL *url = [NSURL URLWithString:#"someUrl.com"];
ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
// [theRequest setPostValue:#"" forKey:#"key1"];
[theRequest setPostValue:#"90" forKey:#"key2"];
[theRequest setPostValue:#"150" forKey:#"key3"];
// [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
[theRequest startAsynchronous];
Code (Third method):
NSData *myPostData = [[NSString stringWithFormat:#"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
// NSString *xmlPostData = #"<my xml>";
NSURL *theURL = [NSURL URLWithString:#"someUrl.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:#"application/xml; charset=utf-8" forHTTPHeaderField:#"Content-type"];
// [theRequest setValue:#"UTF-8" forHTTPHeaderField:#"Charset"];
[theRequest setHTTPBody:myPostData];// [xmlPostData dataUsingEncoding:NSUTF8StringEncoding]];// myPostData];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
How about all these -(void)connection: style functions? Does any of them have to contain specific code for the connection to work?
Many thanks for any help!
NSURL *url = [NSURL URLWithString:#"http://myURL.com"];
ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *myPostData = [[NSString stringWithFormat:#"<My XML Body>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];
[request addRequestHeader:#"Content-Type" value:#"application/xml"];
[request setPostBody:myMutablePostData];
[request setDelegate:self];
[request startSynchronous];

ASIHTTPRequest UIWebView load documents

What is the best way to view documents (pdf,doc,xls) using AsiHttpRequest and UIWebView??? I tried the following, but the UIWebView is displaying the html:
NSString * baseURL = #"http://xxxxxx/open-api/v1/";
NSString * itemRef = #"item/133/attachment/test.pdf";
NSString *urlString = [NSString stringWithFormat:#"%#%#", baseURL, itemRef];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:#"xx"];
[request setPassword:#"xx"];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
[self.webView loadHTMLString:[request responseString] baseURL: [request url]];
}
else {
NSLog(#"Error: %#", error );
}
AsiHttpRequest is required for setting Basic Authentication and Header Values... Thanks!
Well, to be fair, you're telling it to display HTML, so the result isn't really unexpected :-)
You'd need to download the pdf file locally:
NSString *tmpLocation = // some temporary file location
[request setDownloadDestinationPath:tmpLocation]];
[request startSyncronous];
then view it in UIWebView:
NSURL *url = [NSURL fileURLWithPath:tmpLocation];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];

Is the ASIHTTPRequest is not asynchronous?

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