Friday, 18 October 2013

JSON Retrive from server

Format 1:

NSError *er;

NSDictionary  *Dic=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"someURL"]]] options:NSJSONReadingMutableContainers error:&er];


Format 2:

NSString *urlString = [NSString stringWithFormat:@"%@loadPeople",ServiceUrl];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data1 =[NSData dataWithContentsOfURL:url];
    NSString *json_string = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
    
   // NSLog(@"json_string:%@",json_string);
    
       NSDictionary *dicc= (NSDictionary *) [parser objectWithString:json_string error:nil];

FORMAT3:


 NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod: @"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    //NSString *myRequestString = [NSString stringWithFormat:@"countryId=1"];
    NSString *myRequestString = [NSString stringWithFormat:@"countryId=%@",countryId];
    //NSLog(@"test:%@",myRequestString);
    // [myRequestString stringByAppendingString:myParamString];
    NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];
    [request setHTTPBody: requestData];
 NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init];
    NSError* error = [[NSError alloc] init] ;
    
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error)
    {
        
    }
    
    NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
                                                        length:[responseData length]
                                                      encoding:NSUTF8StringEncoding];
    //NSLog(@"responseString:%@", responseString);
    

//    NSString *response1 = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
//    NSLog(@"response:%@",response1);
    
    SBJsonParser *parser = [[SBJsonParser alloc]init];
       
    NSMutableArray jason_array=[parser objectWithString:responseString];

Navigation bar button set Image


    UIImage* image3 = [UIImage imageNamed:@"homeicon25.png"];// creat uiimage
    CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);//set size
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];/create UIButton
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];//set Button On Image
    [someButton addTarget:self action:@selector(hoome)
         forControlEvents:UIControlEventTouchUpInside];//button Calling Method
    [someButton setShowsTouchWhenHighlighted:YES];
    
    UIBarButtonItem *home_button =[[UIBarButtonItem alloc] initWithCustomView:someButton];

    self.navigationItem.rightBarButtonItem=home_button;//Assign UIBArbuttonitem