Rectangle 27 0

ios How to POST data using JSON service in Objective c?

/*********See this**********/

NSString *dataToSend = [NSString stringWithFormat:@"Username=%@&Password=%@,<userIdEnter Here>,<Password enter here>]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
NSString *Length = [NSString stringWithFormat:@"%d",[postData length]]; 

[request setURL:[NSURL URLWithString:@WEBURL]]; 
[request setHTTPMethod:@"POST"]; 

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

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

[request setHTTPBody:postData];

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

// check connection if you want

/*****get response in delegates*******/

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:
  (NSURLResponse *)response {
    // A response has been received, this is where we initialize the instance var you created
    // so that we can append data to it in the didReceiveData method
    // Furthermore, this method is called each time there is a redirect so reinitializing it
    // also serves to clear it

    _responseData = [[NSMutableData alloc] init];

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
  NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data

   // NSArray* latestLoans = [json objectForKey:@"loans"];

    NSLog(@"json: %@", json);

    [_responseData appendData:data];

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
 NSLog(@"Error --> %@",error.localizedDescription);
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];

 NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
// use Result
self.responseData = nil;


First you have to paste the code in class where you want to call web service. now, call method webServiceCall on login Button click, where you have to change user_id and password entered by user. After call happened, 'NSUrlConnection' delegate will called(read delegate documentation). After getting response in connectionDidFinishLoading. You have to parse data in this way ` id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; `. After that you can use 'result'

Seriously I'm not understanding it can u elaborate the code please? Where to write this code how we get response and where to write response delegate? @ajjjjjjjj

what to write in Response delegates? @ajjjjjjjj