Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2011 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fce50a3c4d20cb9c4677 to your computer and use it in GitHub Desktop.
Save anonymous/fce50a3c4d20cb9c4677 to your computer and use it in GitHub Desktop.
Code for calling the webservice parsing and listing in table view!!
//
// AlarmListViewController.h
// WebServiceTab
//
// Created by OSSTeam on 15/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AlarmListViewController : UITableViewController {
NSMutableData* *dataWebService;
}
@end
//
// AlarmListViewController.m
// WebServiceTab
//
// Created by OSSTeam on 15/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "AlarmListViewController.h"
#import "AlarmWSParse.h"
@implementation AlarmListViewController
NSMutableArray* severity;
NSMutableArray* test;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
test= [[NSMutableArray alloc]init];
[test addObject:@"one"];
[test addObject:@"two"];
NSLog(@"test array%@",test);
severity= [[NSMutableArray alloc]init];
[severity retain];
dataWebService = [[NSMutableData data] retain];
NSString *authString = [[[NSString stringWithFormat:@"%@:%@",@"admin", @"admin"] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://172.16.3.47:8980/opennms/rest/alarms?limit=5"]] retain];
[request setValue:[NSString stringWithFormat:@"Basic %@",authString] forHTTPHeaderField:@"Authorization"];
//NSURLConnection* myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
//[myConnection start];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[dataWebService appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Eror during connection: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString* responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
//[dataWebService release];
AlarmWSParse* alarmParser = [[AlarmWSParse alloc]init];
severity = [alarmParser xmlParser:responseString];
NSLog(@"no of elements in array %d",[severity count]);
NSLog(@"Back to Alarm List View controller and severtiy array is %@",severity);
[self.tableView reloadData];
}
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [severity count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"in table view cell for row at indexpath method");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
//NSString* cellValue = [severity objectAtIndex:indexPath.row];
NSString* cellValue = [severity objectAtIndex:indexPath.row];
//NSLog(@"cellvalue %@",cellValue);
cell.textLabel.text = cellValue;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
//
// WebServiceTabAppDelegate.h
// WebServiceTab
//
// Created by OSSTeam on 15/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WebServiceTabAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
//
// WebServiceTabAppDelegate.m
// WebServiceTab
//
// Created by OSSTeam on 15/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "WebServiceTabAppDelegate.h"
@implementation WebServiceTabAppDelegate
@synthesize window;
@synthesize tabBarController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark UITabBarControllerDelegate methods
/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
*/
/*
// Optional UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
*/
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
@end
//
// AlarmWSParse.h
// WebServiceTab
//
// Created by OSSTeam on 19/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface AlarmWSParse : NSObject {
//NSMutableArray* serverityArray;
//NSString* severity;
}
//@property NSMutableArray* severityArray;
//@property NSString* severity;
-(NSMutableArray*) xmlParser:(NSString*)xml;
@end
//
// AlarmWSParse.m
// WebServiceTab
//
// Created by OSSTeam on 19/12/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "AlarmWSParse.h"
#import "TBXML.h"
@implementation AlarmWSParse
//@synthesize severityArray;
NSMutableArray* severityArray ;
NSString* severity;
//@synthesize severity;
-(NSMutableArray*) xmlParser:(NSString*)xml{
severityArray = [[NSMutableArray alloc] init];
NSData* data = [xml dataUsingEncoding:NSUTF8StringEncoding];
[xml release];
TBXML* tbxml = [TBXML tbxmlWithXMLData:data];
[data release];
TBXMLElement* alarmRoot = tbxml.rootXMLElement;
TBXMLElement* alarmElement = [TBXML childElementNamed:@"alarm" parentElement:alarmRoot];
do {
NSLog(@"do while loop%@",[TBXML elementName:alarmElement]);
severity = [TBXML valueOfAttributeNamed:@"severity" forElement:alarmElement];
NSLog(@"severity is %@", severity);
[severityArray addObject:severity];
//NSLog(@"%@",severityArray);
//TBXMLElement* logMsgElement = [TBXML childElementNamed:@"logMessage" parentElement:alarmElement];
//NSLog(@"log msg %@",[TBXML textForElement:logMsgElement]);
} while ((alarmElement=alarmElement->nextSibling));
//NSLog(@"array of elements :%@",severityArray);
[tbxml release];
[severity release];
return severityArray;
}
@end
[Session started at 2011-12-22 17:15:04 +0530.]
2011-12-22 17:15:05.896 WebServiceTab[12995:207] test array(
one,
two
)
2011-12-22 17:15:06.074 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.075 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.075 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.075 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.076 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.076 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.076 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.077 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.077 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.077 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.078 WebServiceTab[12995:207] no of elements in array 5
2011-12-22 17:15:06.078 WebServiceTab[12995:207] Back to Alarm List View controller and severtiy array is (
MAJOR,
MAJOR,
MAJOR,
MAJOR,
MAJOR
)
[Session started at 2011-12-22 17:15:04 +0530.]
2011-12-22 17:15:05.896 WebServiceTab[12995:207] test array(
one,
two
)
2011-12-22 17:15:06.074 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.075 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.075 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.075 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.076 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.076 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.076 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.077 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.077 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.077 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.078 WebServiceTab[12995:207] no of elements in array 5
2011-12-22 17:15:06.078 WebServiceTab[12995:207] Back to Alarm List View controller and severtiy array is (
MAJOR,
MAJOR,
MAJOR,
MAJOR,
MAJOR
)
[Session started at 2011-12-22 17:15:04 +0530.]
2011-12-22 17:15:05.896 WebServiceTab[12995:207] test array(
one,
two
)
2011-12-22 17:15:06.074 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.075 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.075 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.075 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.076 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.076 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.076 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.077 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.077 WebServiceTab[12995:207] do while loopalarm
2011-12-22 17:15:06.077 WebServiceTab[12995:207] severity is MAJOR
2011-12-22 17:15:06.078 WebServiceTab[12995:207] no of elements in array 5
2011-12-22 17:15:06.078 WebServiceTab[12995:207] Back to Alarm List View controller and severtiy array is (
MAJOR,
MAJOR,
MAJOR,
MAJOR,
MAJOR
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment