嗨,我是 iOS 开发的新手。在上面的代码中,我调用了一个 Web 服务,它的响应是 json 格式。我想用该 Web 服务的响应进行 json 解析。
在这里,为什么 json 和 registartion 的值在控制台中显示为 null 请帮助我。
这是我的 WebService.h 文件
@interface WebService : NSObject<NSURLConnectionDelegate ,NSURLConnectionDataDelegate> {
NSMutableData *data;
NSArray * registrationDetail;
}
-(void)callWebService;
@end
这是我的网络 WebService.m 文件
@implementation WebService
-(void)callWebService {
NSString *JsonString =[ NSString stringWithFormat:@"http://bia.igreentechservices.com/ws/ws_registration.php?emailid=%@&password=%@&fname=%@&lname=%@&gender=%@&birthdate=%@&mobile=%@&address=%@&emergency_contact_no=%@&other_contact_no_1=%@&other_contact_no_2=%@",emailAdd,pwd,fName,lName,gen,bDate,mobileNum,add,emergencyContactNum,anotherContactNum1,anothercontactNum2]
UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:JsonString];
NSURLRequest *request =[NSURLRequest requestWithURL:url];
NSURLConnection *myConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
[myConnection start];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
data= [[NSMutableData alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[data appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *responseString = [[NSString alloc]
initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
registrationDetail = [json objectForKey:@"registration"];
NSLog(@"%@",json);
NSLog(@"%@",registrationDetail);
NSLog(@"%@",responseString);
NSLog(@"%@",data);
}