mani chandran mani chandran - 20 days ago 7
Objective-C Question

How to pass the String values to Web services with JSON Objective C

i am new to IOS , i have a tableview in which it displays questions in two sections, and i have two buttons in each cell, Now i want to post Yes & No to the server,

i need to post both question and answer.

My Json data is



{"job_no":"272","job_name":"TEST IOS APP","job_location":"8 street ","yes_questions":{"0":{"question_id":"1","question_title":" Do I understand the job thoroughly?","question_answer":"Yes","question_type":"Full"},"1":{"question_id":"2","question_title":"Am I authorized, qualified & trained to do the work?","question_answer":"Yes","question_type":"Short"},"2":{"question_id":"3","question_title":"Do I have the correct tools & they\u2019re in good condition?","question_answer":"Yes","question_type":"Full"},"3":{"question_id":"4","question_title":"Have I informed others who may be affected by my work? ","question_answer":"Yes","question_type":"Full"},"4":{"question_id":"5","question_title":"Is there free access to others not involved in the vicinity?","question_answer":"Yes","question_type":"Full"},"5":{"question_id":"6","question_title":" I have read & understand the SWMS applicable to this task? ","question_answer":"Yes","question_type":"Full"},"6":{"question_id":"7","question_title":"Have I obtained all permits & filled out correct paperwork? ","question_answer":"Yes","question_type":"Short"},"7":{"question_id":"8","question_title":"Have I applied lock out tags (where & if required)? ","question_answer":"Yes","question_type":"Full"},"8":{"question_id":"9","question_title":" Have I performed a pre job on-site inspection?","question_answer":"Yes","question_type":"Full"},"9":{"question_id":"10","question_title":" Am I fit for this task? (fatigue, injury, medications)","question_answer":"Yes","question_type":"Full"},"10":{"question_id":"11","question_title":"Do I have the appropriate PPE & in good condition?","question_answer":"Yes","question_type":"Full"},"11":{"question_id":"12","question_title":"Can I complete the task on my own ? ","question_answer":"Yes","question_type":"Full"},"27":{"question_id":"40","question_title":"dOUBLE UP TEST","question_answer":"Yes","question_type":"Full"},"28":{"question_id":"42","question_title":"Triple - after the questions are updated","question_answer":"Yes","question_type":"Short"},"29":{"question_id":"43","question_title":"Fourth day 5","question_answer":"Yes","question_type":"Short"},"30":{"question_id":"44","question_title":"all tools available","question_answer":"Yes","question_type":"Full"},"31":{"question_id":"52","question_title":"what tool missing","question_answer":"Yes","question_type":"Full"}},"no_questions":{"12":{"question_id":"13","question_title":"Is there a risk of falls or falling objects?","question_answer":"No","question_type":"Full"},"13":{"question_id":"14","question_title":"Is there a risk of Plant or Traffic interaction?","question_answer":"No","question_type":"Full"},"14":{"question_id":"15","question_title":" Is there a risk of electrical hazards?","question_answer":"No","question_type":"Full"},"15":{"question_id":"16","question_title":" Is the work in or involve a confined space? ","question_answer":"No","question_type":"Full"},"16":{"question_id":"17","question_title":"Are there hazardous chemicals involved?","question_answer":"No","question_type":"Full"},"17":{"question_id":"18","question_title":" Is there a risk of fire or explosion?","question_answer":"No","question_type":"Full"},"18":{"question_id":"19","question_title":" Does the job involve hot works?","question_answer":"No","question_type":"Short"},"19":{"question_id":"20","question_title":"Is there a risk of environmental damage? ","question_answer":"No","question_type":"Full"},"20":{"question_id":"21","question_title":" Is there a risk of crush injuries? (in,on or between)","question_answer":"No","question_type":"Full"},"21":{"question_id":"22","question_title":"Is there a risk of water or sun hazards?","question_answer":"No","question_type":"Full"},"22":{"question_id":"23","question_title":"Are there any access issues or concerns ","question_answer":"No","question_type":"Full"},"23":{"question_id":"24","question_title":" Are there hazards involving unstable ground?","question_answer":"No","question_type":"Full"},"24":{"question_id":"25","question_title":" Do I need to apply isolation ?.................... ","question_answer":"No","question_type":"Short"},"25":{"question_id":"26","question_title":"Could there be any uncontrolled movement? \u2026...\u2026\u2026.\u2026.. ","question_answer":"No","question_type":"Full"},"26":{"question_id":"27","question_title":"Do I need mechanical lifting or a work at height platform? .. ","question_answer":"No","question_type":"Full"},"32":{"question_id":"53","question_title":"The questions are updated","question_answer":"No","question_type":"Short"}}}





My code is



static NSString *cellIdentifier0 = @"Cell0";
static NSString *cellIdentifier1 = @"Cell1";
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

if (indexPath.section==0) {



UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0 forIndexPath:indexPath];

if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


UILabel *Sno = (UILabel *)[cell viewWithTag:96];

UILabel *QuesLab = (UILabel *)[cell viewWithTag:98];

Sno.text= [NSString stringWithFormat:@"%li",indexPath.row +1];
QuesLab.text=[[respArray objectAtIndex:indexPath.row]valueForKey:@"question_title"];

return cell;

}

else

{
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath];

if (!cell1)
{
cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];
cell1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

UILabel *Sno = (UILabel *)[cell1 viewWithTag:96];
UILabel *QuesLab = (UILabel *)[cell1 viewWithTag:98];
Sno.text= [NSString stringWithFormat:@"%li",indexPath.row +1];
QuesLab.text=[[respArray1 objectAtIndex:indexPath.row]valueForKey:@"question_title"];
return cell1;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}







- (IBAction)yesBtnAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [self getButtonIndexPath:sender];
NSLog(@"Index section : %li", indexPath.section);
NSLog(@"IndexPath row : %li", indexPath.row);
UIButton *yesBTN=(UIButton *)[[sender superview]viewWithTag:100];
UIButton *noBTN=(UIButton *)[[sender superview]viewWithTag:111];
[noBTN.layer setBackgroundColor:[[UIColor whiteColor] CGColor]];
[yesBTN setBackgroundColor:[UIColor colorWithRed:0.23 green:0.62 blue:0.23 alpha:1.0]];
}

-(NSIndexPath *) getButtonIndexPath:(UIButton *) button
{
CGRect buttonFrame = [button convertRect:button.bounds toView:take5table];
return [take5table indexPathForRowAtPoint:buttonFrame.origin];
}

- (IBAction)noBtnAction:(id)sender
{
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview;
UITableView *tableView = (UITableView *)cell.superview;
NSIndexPath *indexPath = [self getButtonIndexPath1:sender];
NSLog(@"Index section : %li", indexPath.section);
NSLog(@"IndexPath row : %li", indexPath.row);
UIButton *yesBTN=(UIButton *)[[sender superview]viewWithTag:100];
UIButton *noBTN=(UIButton *)[[sender superview]viewWithTag:111];
[noBTN.layer setBackgroundColor:[[UIColor redColor] CGColor]];
[yesBTN setBackgroundColor:[UIColor whiteColor]];

}
-(NSIndexPath *) getButtonIndexPath1:(UIButton *) button
{
CGRect buttonFrame = [button convertRect:button.bounds toView:take5table];
return [take5table indexPathForRowAtPoint:buttonFrame.origin];
}











- (IBAction)ContinueAucit:(id)sender
{
NSString *post = [NSString stringWithFormat:@"jobid=%@&id=%@&questionarray=%@",idforjob,user_idis,question_array];

NSLog(@"%@",post);

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:@"http://10.10.1.5/webservices_api/process"]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(conn)
{
NSLog(@"Connection Successful");
}

else
{
NSLog(@"Connection could not be made");
}
}





My screen is
enter image description here

I want the format like :[Question,Original_Ans,User_entered_ans]

Answer

once try this let me know any issues

- (IBAction)YesBtnAction:(id)sender

{
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    
    NSIndexPath *indexPath = [self.tableview indexPathForCell:clickedCell];
    
    int arrayCount = 0;
    
    for (int i=0; i<indexPath.section; i++)
    {
        arrayCount +=  [[dicc valueForKey:[headersArray objectAtIndex:i]]count];
    }
    
    array[arrayCount + indexPath.row] = @"1";

    NSLog(@" yes Button Clicked by user %ld",(unsigned long)[array count]);
    
       [tableview reloadData];
   
}

- (IBAction)NoBtnAction:(id)sender
{

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    
    NSIndexPath *indexPath = [self.take5Table indexPathForCell:clickedCell];
    
    int arrayCount = 0;
    
    for (int i=0; i<indexPath.section; i++)
    {
        arrayCount +=  [[dicc valueForKey:[headersArray objectAtIndex:i]]count];
    }
    
    array[arrayCount + indexPath.row] = @"0";
    
    NSLog(@" No Button Clicked by user %ld",(unsigned long)[array);
    
    [tableview reloadData];

}

- (IBAction)ContinueAudit:(id)sender
{
    NSUInteger arrayCount;
    
    NSMutableDictionary *question_array = [[NSMutableDictionary alloc]init];
    
    int k = 0;
    
    for (int i=0; i<dicc.count; i++) {
        
        NSMutableDictionary *answerDict = [[NSMutableDictionary alloc]init];
        
        arrayCount =  [[dicc valueForKey:[headersArray objectAtIndex:i]]count];
        
        for (int j=0; j<arrayCount; j++)
        {
            int x = k + j;
            
            // NSString * key = [[dicc valueForKey:[headersArray objectAtIndex:i]]valueForKey:@"question_title"];
            
         NSString *key = [NSString stringWithFormat:@"%d", j];
            
            answerDict[key] =  array[x];
        }
      NSString *key = [NSString stringWithFormat:@"%d", i];
        
        question_array[key] = answerDict;
        
        k += arrayCount;
    }
    NSString *post = assign your code here ;
    NSLog(@"%@",post);
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
    
    //NSUTF8StringEncoding
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
   [request setURL:[NSURL URLWithString:url here ];
    
    [request setURL:[NSURL URLWithString:@""]];
    
    [request setHTTPMethod:@"POST"];
    
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    [request setHTTPBody:postData];
    
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if(conn)
    {
        
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection could not be made");
    }
}

Comments