Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

How to use ASIHTTPRequest for Sync Http Request?

$
0
0

What is NSError

Sending Synchronous HTTP Request using AISHTTP library is very easy. Since the request is synchronous, no callback method is required for the same. You can specify the request timeout as desired. The following snippet shows how to send and get the sync request and get the response.  <!--break-->

Asychornous Http Request with ASIHTTP

  1. The sendAndReceive method sends the request.
  2. -(GDataXMLDocument*) sendAndReceive:(NSString *) restUrl withError:(NSError**) error
  3. {
  4.  debugLog(@"URL : %@",restUrl);
  5.  ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:[NSURL URLWithString:restUrl]];
  6.  
  7.  [request setTimeOutSeconds:REQUEST_TIMEOUT];
  8.  
  9.  [request setAllowCompressedResponse:NO];
  10.  [request setDefaultResponseEncoding:NSUTF8StringEncoding];
  11.  [request startSynchronous];
  12.  
  13.  NSError *httpError =[request error];
  14.  if(!httpError){
  15.  
  16.  NSString *responseXml =[[request responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  17.  
  18.  // responseXml = @"<!--?xml version=\"1.0\" encoding=\"UTF-8\"?-->10";
  19.  
  20. // NSData *data = [responseXml dataUsingEncoding: NSUTF8StringEncoding];
  21.  
  22.  debugLog(@"response %@", responseXml);
  23.  GDataXMLDocument *doc =[[GDataXMLDocument alloc] initWithXMLString:responseXml options:0 error: error];
  24.  return doc;
  25.  }
  26.  else
  27.  {
  28.  error =&httpError;
  29.  returnNULL;
  30.  }
  31.  
  32. }

Best practices stipulate that HTTP request should not block so make the GUI responsive (enhance user experience) so if you can consider running http request in Async mode.


Viewing all articles
Browse latest Browse all 178

Trending Articles