Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Asynchronous URL Request?

Tags:

I would like to know how do I get a return value 1 or 0 only.... back from an URL request asynchronously.

currently I do it in this way:

NSString *UTCString = [NSString stringWithFormat:@"http://web.blah.net/question/CheckQuestions?utc=%0.f",[lastUTCDate timeIntervalSince1970]]; NSLog(@"UTC String %@",UTCString); NSURL *updateDataURL = [NSURL URLWithString:UTCString]; NSString *checkValue = [NSString stringWithContentsOfURL:updateDataURL encoding:NSASCIIStringEncoding error:Nil]; NSLog(@"check Value %@",checkValue); 

this works, however it is blocking my main thread till I got a reply back from the URL, how do I set it so it will do it in a another thread instead of the main thread ?

EDIT: ANSWER I end upcalling my function with this, it works well :)

[self performSelectorInBackground:@selector(shouldCheckForUpdate) withObject:nil]; 
like image 633
Desmond Avatar asked Dec 15 '11 05:12

Desmond


People also ask

How do you send asynchronous request?

You can also send requests synchronously by calling WdfRequestSend, but you have to format the request first by following the rules that are described in Sending I/O Requests Asynchronously. Sending I/O requests to an I/O target synchronously is simpler to program than sending I/O requests asynchronously.

What is an asynchronous HTTP request?

Asynchronous HTTP Request Processing is a relatively new technique that allows you to process a single HTTP request using non-blocking I/O and, if desired in separate threads. Some refer to it as COMET capabilities.

Which method is used to make an asynchronous HTTP request?

Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method.

Is HTTP POST synchronous or asynchronous?

HTTP is a synchronous protocol. The client sends a request and waits for a response from the service. That's independent of the client code execution that could be synchronous (thread is blocked) or asynchronous (thread isn't blocked, and the response will reach a callback eventually).


2 Answers

you can use NSURLConnection class

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 

and handle its response and errors using its delegate methods.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error - (void)connectionDidFinishLoading:(NSURLConnection *)connection  

You can find implementation of NSURLConnection

  • Apple docs: Using NSURLConnection
  • How To Use iOS NSURLConnection By Example

Edit: Although NSURLConnection is provided by apple is more recommended way of placing URL request. But I found AFNetworking library very time saving, easy to implement and robust yet simple as third party implementation. You should give it a try.

like image 184
Adil Soomro Avatar answered Oct 18 '22 17:10

Adil Soomro


try this :

.h:

NSMutableData *responseData; 

.m:

- (void)load  {   NSURL *myURL = [NSURL URLWithString:@"http://www.example.com"];   NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];    [[NSURLConnection alloc] initWithRequest:request delegate:self]; }  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  {   responseData = [[NSMutableData alloc] init]; }  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  {   [responseData appendData:data]; }  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error  {   [responseData release];   [connection release];   [textView setString:@"Unable to fetch data"]; }  - (void)connectionDidFinishLoading:(NSURLConnection *)connection  {   NSLog(@"Succeeded! Received %d bytes of data",[responseData                                                    length]);   NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; } 
like image 43
Maulik Avatar answered Oct 18 '22 17:10

Maulik