PDA

View Full Version : NSURLConnection




sujithkrishnan
Jun 20, 2008, 02:29 AM
Hi all.

In my iPhone application, i am making use of

NSURLConnection's (id) initWithRequest: (NSURLRequest*)request delegate: (id)delegate three times sequentially to connect to three xml files.

problem is that i am making use of the info from first URL to connect to second URL and info from second URL to connect to thrid and so on..

But as the above method is parsing in thread-model by default, the second connection is tryiong to establish befor the first one get connected...

All three connection are three separate lines in my program...

How to make the first connection to do things before connecting to second?

I mean how to change the thread beahaviour of the above method??

Thanks



therevolution
Jun 20, 2008, 10:00 AM
Take a look at NSURLConnection's sendSynchronousRequest method.

wwwww
Mar 30, 2009, 05:59 AM
Hello,
we are using NSURLConnection to connect web server using
sendSynchronousRequest method.but We are facing so much problem.
1) Request get blocked / Not responding.
2) how to set
Connect Time out : to use for name resolution.
Send Timeout : to use for sending requests
ReceiveTimeout : time is - receive a response to a request.

(NSString*)sendLoadBalenceRequest:(NSString*)XMLString
{
[XMLString retain];
NSLog(@"Sending the Load Balence request to server.");
NSError *myError=nil;
NSHTTPURLResponse *serverResponse=nil;
NSData *serverData;
NSString *serverDataString = nil;
NSDictionary*dic;
NSArray *arr;


@try
{
NSURL*theServerURL = [NSURL
URLWithString:m_strXTCIURL];


NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:theServerURL


cachePolicy:NSURLRequestUseProtocolCachePolicy


timeoutInterval:10.0];
[theRequest setValue:@"text/xml"
forHTTPHeaderField:@"Content-
Type"];


[theRequest setHTTPMethod:@"POST"];


[theRequest setHTTPBody:[XMLString
dataUsingEncoding:NSUTF8StringEncoding]];


NSURLConnection *conn = [NSURLConnection
connectionWithRequest:theRequest delegate:self];


if(conn == nil)
{
NSRunCriticalAlertPanel(NSLocalizedString
(@"Error",NULL),@"Connection can't establish",NSLocalizedString
(@"OK",NULL),NULL,NULL);
return @"";
}
serverData = [NSURLConnection
sendSynchronousRequest:theRequest


returningResponse:&serverResponse


error:&myError];


if(myError!=NULL)
{
NSLog(@"\n\nERROR: %@",[myError
localizedDescription]);
NSLog(@"\n\nERROR code: %d",[myError code]);
NSLog(@"\n\nERROR domain: %@",[myError
domain]);


[self errorHandling:[NSString
stringWithFormat:@"%d", [myError
code]] strErrorText:[myError localizedDescription]];


dic = [myError userInfo];
arr = [dic allValues];


int i = 0;
for(i = 0 ; i< [arr count]; i++)
{
NSLog(@"\n%d. %@ \n",i+1,[arr
objectAtIndex:i]);


}
serverDataString = @"";
}
else
{
serverDataString = [[[NSString alloc]
initWithData:serverData
encoding:NSUTF8StringEncoding] retain];
NSLog(@"Server Response :\n%@
\n",serverDataString);
}


} @catch (id e)
{
NSLog(@"EXCEPTION: %@ ",e);
} @finally
{
[XMLString release];
return[serverDataString autorelease];
}



}


Problem::
In this function we have used api sendSynchronousRequest, Which is
blocking if it don't get response, or in case of heavy load, or if
network connection is not proper.

kainjow
Mar 30, 2009, 08:02 AM
Don't use sendSynchronousRequest. Use the asynchronous methods instead, which will solve your problems.

The timeout is set in NSURLRequest, but it's not guaranteed to work every time (at least I haven't had that experience). You may need to set a timer to cancel.