先把mysql的php接口定义为宏
#define kSqlUrl @"http://localhost:8888/mysql.php"
.m文件
demo
@interface YcwViewController ()
{
NSMutableArray *array;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
array = [NSMutableArray array];
NSURL *url = [NSURL URLWithString:kSqlUrl];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError != nil) {
NSLog(@"请求失败!,%@",[connectionError localizedDescription]);
}
else
{
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",arr);
for (NSDictionary *dic in arr) {
Position *position = [[Position alloc]init];
position.name = [dic objectForKey:@"name"];
position.money = [dic objectForKey:@"award"];
position.place = [dic objectForKey:@"city"];
position.position_id = [dic objectForKey:@"id"];
[array addObject:position];
}
NSLog(@"%@",array);
[self.myTableView reloadData];
}
}];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
}
demo2
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [NSString stringWithFormat:@"http://localhost:8888/show.php?id=%@",self.position_id]; //get请求可以直接把数据跟在url地址后面
//其他步骤和网络请求一样
NSURL *url = [NSURL URLWithString:path];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError != nil) { NSLog(@"请求失败!,%@",[connectionError localizedDescription]); } else { NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSDictionary *dic = arr[0]; NSLog(@"%@",dic); self.myLabel.text = [NSString stringWithFormat:@"name = %@ city = %@ award = %@ company = %@ descripe = %@ education = %@ experience = %@ kind = %@ offer =%@",[dic objectForKey:@"name"],[dic objectForKey:@"city"],[dic objectForKey:@"award"],[dic objectForKey:@"company"],[dic objectForKey:@"descripe"],[dic objectForKey:@"education"],[dic objectForKey:@"experience"],[dic objectForKey:@"kind"],[dic objectForKey:@"offer"]]; } }]; // Position *position = [array objectAtIndex:0]; // self.myLabel.text = [NSString stringWithFormat:@"%@ %@ %@ ",position.money,position.name,position.place]; }