PDA

View Full Version : Problem calling web service from another class




Amit Jain
Jul 6, 2009, 08:37 AM
Hi All,
I have a problem in calling a web service from another class.

I am taking reference from Hello_Soap Example. Whenever I call a web service as shown in example it works fine and calls following methods internally.

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
-(void)connectionDidFinishLoading:(NSURLConnection *)connection

now I want that the web service should be called by a function call from other class. Whenever I call the function web service gets called but the control goes back to calling class without executing above 3 methods and the parsing.
I want the call to web service and parsing to be in a separate class.
These methods are not getting called. How to do it?
Thanks in advance.
Amit



drivefast
Jul 6, 2009, 02:00 PM
do you create anywhere an object which is an instance of the class that implements the connection functionality?

Amit Jain
Jul 6, 2009, 11:47 PM
do you create anywhere an object which is an instance of the class that implements the connection functionality?

Yes I call the webservice method from another class by making the instance of the class that implements the connection functionality.
Is that the reason ? If yes then how can be call the web service method without making an instance?
Please help.
regards..
Amit

drivefast
Jul 7, 2009, 09:49 AM
no, youre doing the right thing. another thing that i can think of is to make sure you set the delegate of the NSURLConnection object as you want it - in your case to an instance of your parsing class, if i understood correctly.

Amit Jain
Jul 8, 2009, 01:48 AM
no, youre doing the right thing. another thing that i can think of is to make sure you set the delegate of the NSURLConnection object as you want it - in your case to an instance of your parsing class, if i understood correctly.

Thanx a lot, we are already doing this in the Webservice Class
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

We have taken self because this Connection methods are written in Parser class only. But still same problem.
I will explain you in detail: there are three classes-> Webservice class, where all parsing and web service calling functionality is written. We have written one method in this class that initiates the webservice call and parsing of XML. The other two classes suppose A & B that want to use this webservice by calling the above method.

//Class A
#import "Webservice.h"
- viewDidLoad
{
Webservice *ws = [[Webservice alloc] init];
[ws callWebservice];
statement 1..
Statement 2..
...
}


Now what happens is [ws callWebservice]; this method gets called but returns without executing the following delegate methods written in Webservice class

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


after executing statement 1, statement 2 in class A , then the control goes back to above these methods.
Please help.
Thnx

Amit Jain
Jul 8, 2009, 11:52 PM
Thanx a lot, we are already doing this in the Webservice Class
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

We have taken self because this Connection methods are written in Parser class only. But still same problem.
I will explain you in detail: there are three classes-> Webservice class, where all parsing and web service calling functionality is written. We have written one method in this class that initiates the webservice call and parsing of XML. The other two classes suppose A & B that want to use this webservice by calling the above method.

//Class A
#import "Webservice.h"
- viewDidLoad
{
Webservice *ws = [[Webservice alloc] init];
[ws callWebservice];
statement 1..
Statement 2..
...
}


Now what happens is [ws callWebservice]; this method gets called but returns without executing the following delegate methods written in Webservice class

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


after executing statement 1, statement 2 in class A , then the control goes back to above these methods.
Please help.
Thnx
Found various answers here, hope it helps someone:
1.https://devforums.apple.com/message/94855#94855
2.http://stackoverflow.com/questions/1096493/calling-web-service-from-another-class
3.http://www.iphonedevsdk.com/forum/iphone-sdk-development/22580-web-service-calling-issue.html#post101233