I tried to establish a connection with a request:
Code:
- (NSMutableData *)createRequest:(NSString *)city {
NSMutableData *myMutableData;
NSMutableString *sRequest = [[NSMutableString alloc] init];
[sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
[sRequest appendString:@"<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/\">"];
[sRequest appendString:@"<soap:Body>"];
[sRequest appendString:@"<GetWeather xmlns=\"http://litwinconsulting.com/webservices/\">"];
[sRequest appendString:@"<City>"];
[sRequest appendString:city];
[sRequest appendString:@"</City>"];
[sRequest appendString:@"</GetWeather>"];
[sRequest appendString:@"</soap:Body>"];
[sRequest appendString:@"</soap:Envelope>"];
NSURL *weatherServiceURL = [NSURL URLWithString:@"http://litwinconsulting.com/webservices/GetWeather"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:weatherServiceURL];
[request addValue:@"text/xml; charset:UTF-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://litwinconsulting.com/webservices/GetWeather" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
myMutableData = [[NSMutableData data] retain];
//NSLog(@"Conn is true");
}
[NSURLConnection connectionWithRequest:request delegate:self];
NSError *WSerror;
NSURLResponse *WSresponse;
myMutableData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&WSresponse error:&WSerror];
return myMutableData;
}
and tried to parse the result:
Code:
- (void)viewDidLoad {
[super viewDidLoad];
[myText setText:@"0"];
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
SOAPRequest *request = [[SOAPRequest alloc] init];
NSData *myData = [[NSData alloc] initWithData:[request createRequest:@"Vienna"]];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:myData];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributDict {
NSLog(@"Found element %@", elementName);
if ([elementName isEqualToString:@"GetWeatherResult"]) {
[myText setText:elementName];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[myText setText:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
qualifiedName:(NSString *)qName {
}
Im not sure if i use the parser correct but the 3 parser methods are never called either.
Any idea?