How to use webservice and parse json response in iphone programming???

Leave a Comment
Here i am explaining you how you will call web service  in iphone and in response it will return in json. So how to parse json response in iphone also.

step 1:

your web service url:
www.websitename.com/services/web-services.php?web_method=login&email=parameter1&password=parameter2

here websitename : webservice website
        parameter1:email id passed by you
        parameter2:password 
step2:
put this webservice url inbrowser it will return response like this::


{"login":[{"user_id":"11","user_name":"devang","auth_token":"7fe890ee-c226-11e2-ae91-782bcb3e4778","level":"0"}]}
step3:
 How to call this webservice and parse response

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www. websitename.com/services/web-services.php?web_method=login&email=%@&password=%@",txt_username.text,password_txt.text]]];
    [request setHTTPMethod:@"GET"];
  
    [request addValue:@"getValues" forHTTPHeaderField:@"METHOD"];        //selects what task the server will perform
     NSLog(@"Testjsonoutput::%@",request);    //initialize an NSURLConnection  with the request
//    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  
    NSData *returnData = [ NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];      
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];     
    NSLog(@"Testjsonoutput::%@",returnString);     
     NSMutableDictionary *dict = [returnString JSONValue];

     NSMutableArray *dict1 =[dict valueForKey:@"login"];
  
     NSMutableDictionary *dict2 = [dict1 objectAtIndex:0];
 
     NSLog(@"Testjsonaddress::%@",[dict2 valueForKey:@"user_name"]);      
     NSLog(@"Testjsonaddress::%@",[dict2 valueForKey:@"auth_token"]); 

Step4:

Now you got all values coming from webservice and use them in your application

Still if you have any doubts than give me your url of service i will give code for it

need any help please comment on it or drop mail on devang9978@gmail.com

ouptut::







0 comments:

Post a Comment