farheen sultana
farheen sultana

Reputation: 1

fetching json from xcode

I am calling webservice written in .net from xcode 4.The output is a json string .when The call is made with url usingstatic ip address it gives error.HTTP Error 404.0 - Not Found.But works when same url is called in localhost.the code is given below

- (void)viewDidLoad
{    
    [super viewDidLoad]; 
    NSURL *url=[NSURLURLWithString:@"http:// static ip address where serviceis located(169.254.216.8)/restGroceryService/WebService/insertList?prods=apples,bananas,milk"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *pageSource = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"page source%@",pageSource);
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data  options:kNilOptions error:nil];
    NSArray *array = [json objectForKey:@"result"]; 
    NSDictionary *store = [array objectAtIndex:0];
    NSNumber *storeId = [store objectForKey:@"storeId"];
    NSNumber *total = [store objectForKey:@"TOTAL"];
    self.priceTextField.text =[total stringValue];
    NSString *storeName = [store objectForKey:@"storeName"];
    self.storNameTextField.text = storeName;
} 

Upvotes: 0

Views: 457

Answers (1)

AlessioCorvaglia.com
AlessioCorvaglia.com

Reputation: 13

ithink the error was here try now:


NSURL *url=[NSURL URLWithString:@"your address"];/<------ here was the error
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *pageSource = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"page source%@",pageSource);
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data  options:kNilOptions error:nil];
    NSArray *array = [json objectForKey:@"result"];
    NSDictionary *store = [array objectAtIndex:0];
    NSNumber *storeId = [store objectForKey:@"storeId"];
    NSNumber *total = [store objectForKey:@"TOTAL"];
    self.priceTextField.text =[total stringValue];
    NSString *storeName = [store objectForKey:@"storeName"];
    self.storNameTextField.text = storeName;

Upvotes: 1

Related Questions