How can I improve this code?
This question is related to What is the last function in iPhone application lifecycle
-(void)LogoutUser
{
int userId = [[GlobalData sharedMySingleton] getUserId];
NSString *soapMsg =
[NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>...", userId
];
NSURL *url = [NSURL URLWithString: #"http://....asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:#"http://..." forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
webData = [[NSMutableData data] retain];
}
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response
{
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
[theXML release];
[connection release];
[webData release];
}
The problem is that when your application switches to the background state, it no longer receives any network updates. If you require the network call to return before the application enters the background, maybe a synchronous call will help you there.
If you decide to go this route, I also suggest to look at ASIHTTPRequest because those classes will allow you to set a timeout on the synchronous call while the normal NSURLConnection classes won't. Otherwise you risk that your application will be terminated by the iOS if the server does not respond.
Related
I am learning parsing in iPhone, When i was searching for the tutorials, i came to know very little bit about SOAP and REST services.
Now i wanted to learn SOAP and REST services.
Do any one have theoretical knowledge of SOAP and REST?
Is there any simple tutorial available for the beginners?
Thanks a lot in advance.
see these links, alongwith comparison of both
http://www.ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest
http://greatgandhi.wordpress.com/2010/06/16/soap-vs-rest-%E2%80%93-the-best-webservice/
http://www.petefreitag.com/item/431.cfm
SOAP GUIDE
http://sudzc.com/
REST GUIDE
http://stackoverflow.com/questions/630306/iphone-rest-client
SOAP Web Service
-(IBAction) read_data
{
NSString *soapMessage = [NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">n"
"<Celsius>%#</Celsius>n"
"</CelsiusToFahrenheit>n"
"</soap:Body>\n"
"</soap:Envelope>\n",name.text
];
NSLog(#"soap Message= %#",soapMessage);
NSURL *url = [NSURL URLWithString:#"your URL "];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"theXML %#",theXML); // Here is your received Value from url
[theXML release];
}
Here is the Example of RESTful WebService
I am new to iPhone app development. How can I post data and get data by accessing the asmx web service using iPhone app?
I think asmx webservices are SOAP webservices you should read my blog entry here -
http://www.makebetterthings.com/iphone/call-soap-web-service-from-iphone/
To call a SOAP service first i create a string with the SOAP request as follows.
NSString *soapMessage = #"<?xml version="1.0" encoding="utf-8"?>n"
"<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n"
"<soap:Body>n"
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">n"
"<Celsius>50</Celsius>n"
"</CelsiusToFahrenheit>n"
"</soap:Body>n"
"</soap:Envelope>n";
After creating the SOAP request I create a NSMutableRequest to send this request to server.
NSURL *url = [NSURL URLWithString:#"http://w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: #"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:#"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
After firing the request we can collect the XML response in the NSURLConnection’s delegate methods.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"%#",theXML);
[theXML release];
}
After collecting the XML response in theXML string in -(void)connectionDidFinishLoading:(NSURLConnection *)connection
we can parse this string using TBXML for any other XML parser you like.
I am updating my data but it not updating why it happen
i want if any register user want to change data old to new then i give a update page where the user change there data but except username i try lot byt it's not updata data to server databasetable my cod is proper or wrong
-(void)sendRequest
{
NSString *post = [NSString stringWithFormat:#"firstname=%#&lastname=%#&Username=%#&Password=%#&Email=%#",txtfirstName.text,txtlast.text,txtUserName.text,txtPassword.text,txtEmail.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSLog(#"%#",postLength);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://192.168.0.1:96/JourneyMapperAPI?RequestType=Register&Command=SET"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(#"%#",webData);
}
else
{
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"%#",loginStatus);
}
you need to start the connection:
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(#"%#",webData);
[theConnection start];
}
else
{
}
I am new to web services in iphone.
I fallow this tutorial
my code is as fallows
NSString *soapMsg =
[NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GenericAndroidMethod xmlns=\"Mortgage\">"
"<methodName>MortgageGetLoanOfficerInfo</methodName>"
"<xmlParam><MortgageGetLoanOfficerInfo><PhoneNumber>919703661366</PhoneNumber></MortgageGetLoanOfficerInfo></xmlParam>"
"</GenericAndroidMethod>"
"</soap:Body>"
"</soap:Envelope>"
];
NSLog(#"%#",soapMsg);
NSURL *url = [NSURL URLWithString:#"http://10.10.8.7/MobileGenericWebservice/GenericWebService.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
[req addValue:#"text/xml; charset=utf-8"
forHTTPHeaderField:#"Content-Type"];
[req addValue:#"Mortgage/GenericAndroidMethod"forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength
forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
NSLog(#"connected %#",conn);
webData = [[NSMutableData data] retain];
}
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
NSLog(#"connect %#",connection);
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
NSLog(#"test %#",data);
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
NSLog(#"error will occer %#",error);
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSLog(#"recived data %#",webData);
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(#"%#",theXML);
[theXML release];
[connection release];
[webData release];
}
In Console i got
2010-12-20 11:23:50.754 Hello_SOAP[1911:40b] <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GenericAndroidMethod xmlns="Mortgage"><methodName>MortgageGetLoanOfficerInfo</methodName><xmlParam><MortgageGetLoanOfficerInfo><PhoneNumber>919703661366</PhoneNumber></MortgageGetLoanOfficerInfo></xmlParam></GenericAndroidMethod></soap:Body></soap:Envelope>
2010-12-20 11:23:50.963 Hello_SOAP[1911:40b] connected <NSURLConnection: 0x5d3dc30, http://10.10.8.7/MobileGenericWebservice/GenericWebService.asmx>
2010-12-20 11:23:51.375 Hello_SOAP[1911:40b] connect <NSURLConnection: 0x5d3dc30, http://10.10.8.7/MobileGenericWebservice/GenericWebService.asmx>
2010-12-20 11:23:51.376 Hello_SOAP[1911:40b] DONE. Received Bytes: 0
2010-12-20 11:23:51.377 Hello_SOAP[1911:40b] recived data <>
in connection didReceiveData i did n't get any data
can any one pls help me why i did n't receive data.
Thank u in advance.
NSString *soapMsg =
[NSString stringWithFormat:
#"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GenericAndroidMethod xmlns=\"Mortgage\">"
"<methodName>string</methodName>"
"<xmlParam>string</xmlParam>"
"</GenericAndroidMethod>"
"</soap:Body>"
"</soap:Envelope>"
];
NSLog(#"%#",soapMsg);
NSURL *url = [NSURL URLWithString:#"http://173.31.193.92/MobileGenericWebservice/GenericWebService.asmx"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMsg length]];
[req addValue:#"text/xml; charset=utf-8"
forHTTPHeaderField:#"Content-Type"];
[req addValue:#"Mortgage/GenericAndroidMethod"forHTTPHeaderField:#"SOAPAction"];
[req addValue:msgLength
forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
NSLog(#"connected %#",conn);
webData = [[NSMutableData data] retain];
}
}
-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
NSLog(#"connect %#",connection);
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
// NSLog(#"test %#",data);
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
NSLog(#"error will occer %#",error);
[webData release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSLog(#"recived data %#",webData);
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(#"%#",theXML);
[theXML release];
[connection release];
[webData release];
}
The response and the logs are as follows:
2010-12-23 11:39:55.240 backGroundTest[1260:207] <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GenericAndroidMethod xmlns="Mortgage"><methodName>string</methodName><xmlParam>string</xmlParam></GenericAndroidMethod></soap:Body></soap:Envelope>
2010-12-23 11:39:55.245 backGroundTest[1260:207] connected <NSURLConnection: 0x4b4c220, http://173.31.193.92/MobileGenericWebservice/GenericWebService.asmx>
2010-12-23 11:39:56.105 backGroundTest[1260:207] connect <NSURLConnection: 0x4b4c220, http://173.31.193.92/MobileGenericWebservice/GenericWebService.asmx>
2010-12-23 11:39:56.106 backGroundTest[1260:207] DONE. Received Bytes: 530
2010-12-23 11:39:56.107 backGroundTest[1260:207] recived data <3c3f786d 6c207665 7273696f 6e3d2231 2e302220 656e636f 64696e67 3d227574 662d3822 3f3e3c73 6f61703a 456e7665 6c6f7065 20786d6c 6e733a73 6f61703d 22687474 703a2f2f 73636865 6d61732e 786d6c73 6f61702e 6f72672f 736f6170 2f656e76 656c6f70 652f2220 786d6c6e 733a7873 693d2268 7474703a 2f2f7777 772e7733 2e6f7267 2f323030 312f584d 4c536368 656d612d 696e7374 616e6365 2220786d 6c6e733a 7873643d 22687474 703a2f2f 7777772e 77332e6f 72672f32 3030312f 584d4c53 6368656d 61223e3c 736f6170 3a426f64 793e3c47 656e6572 6963416e 64726f69 644d6574 686f6452 6573706f 6e736520 786d6c6e 733d224d 6f727467 61676522 3e3c4765 6e657269 63416e64 726f6964 4d657468 6f645265 73756c74 3e266c74 3b4e6577 44617461 53657426 67743b0d 0a202026 6c743b47 55494454 61626c65 2667743b 0d0a2020 2020266c 743b4755 49442667 743b3635 37343664 33382d39 3464322d 34306433 2d613636 322d3633 31353765 65346263 6436266c 743b2f47 55494426 67743b0d 0a202026 6c743b2f 47554944 5461626c 65266774 3b0d0a26 6c743b2f 4e657744 61746153 65742667 743b3c2f 47656e65 72696341 6e64726f 69644d65 74686f64 52657375 6c743e3c 2f47656e 65726963 416e6472 6f69644d 6574686f 64526573 706f6e73 653e3c2f 736f6170 3a426f64 793e3c2f 736f6170 3a456e76 656c6f70 653e>
2010-12-23 11:39:56.108 backGroundTest[1260:207] <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GenericAndroidMethodResponse xmlns="Mortgage"><GenericAndroidMethodResult><NewDataSet>
<GUIDTable>
<GUID>65746d38-94d2-40d3-a662-63157ee4bcd6</GUID>
</GUIDTable>
</NewDataSet></GenericAndroidMethodResult></GenericAndroidMethodResponse> </soap:Body></soap:Envelope>
I beleive the problem is with the ip setting. check it out again please.
try to see the headers from the didReceiveResponse method
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:#selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
}
see the description. It might give the response header received from the server. It contains Length parameter. If it is 0, the server is returning no data to the app. One possible work around will be to change the header for the request.
Note that the connection didReceiveData is not getting called. There might be a potential error in sending the response from the server. Try and simulate the same testing parameters of the soap service from external links and see the response. You can also verify it in the didReceiveResponse method by extracting the response code for the NSURLResponse.
I beleive the error is due to the inappropriate formatting of the data being sent.
How to make Http Post Request using JSON. I tried every option which is available on Internet.But could not get the Data. So please post entire code to make a request.
You can use below code:
-(void)performRequest{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"url"]];
NSString *msgLength = [NSString stringWithFormat:#"%d", [jsonMessage length]];
[request addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue: jsonAction forHTTPHeaderField:#"JSONAction"];
[request addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody: [jsonMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(#"theConnection is NULL");
}
[pool release];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[webData setLength: 0];
self.resultArray = [[NSMutableArray alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(#"ERROR with theConenction");
NSDictionary *errorDic = [NSDictionary dictionaryWithObject:error forKey:#"error"];
[self.resultArray addObject:errorDic];
[connection release];
[webData setLength:0];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(#"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(#"%#", theXML);
[theXML release];
if([webData length] > 0){
parser = [[NSXMLParser alloc] initWithData:webData];
[parser setDelegate:self];
[parser parse];
}
}
Here's a basic example of NSURLConnection POST-ing JSON to an URL.
- (void)performRequest {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://someplace.com/"]];
[request setValue:#"Some Value" forHTTPHeaderField:#"Some-Header"];
[request setHTTPBody:#"{\"add_json\":\"here\"}"];
[request setHTTPMethod:#"POST"];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Fail..
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Request performed.
}