发个广告:ios开发两年了,一步步走来 关注公众号一起进步
post 数据请求
NSURL *url = [NSURLURLWithString:@"http://172.16.150.131:8081/app/insertAppActionStatistics"];
// 2. 请求(可以改的请求)
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
// ? POST
// 默认就是GET请求
request.HTTPMethod =@"POST";
// ? 数据体
NSString *str = [NSStringstringWithFormat:@"userId=%@&userType=%d&actionType=%d",[DataManagersharedInstance].userId,1,1];
NSLog(@"%@",[DataManagersharedInstance].userId);
// 将字符串转换成数据
request.HTTPBody = [strdataUsingEncoding:NSUTF8StringEncoding];
// 3. 连接,异步
[NSURLConnectionsendAsynchronousRequest:request queue:[[NSOperationQueuealloc] init]completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {
if (connectionError ==nil) {
// 网络请求结束之后执行!
// 将Data转换成字符串
NSString *str = [[NSStringalloc] initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"%@ %@", str, [NSThreadcurrentThread]);
}
}];
get 数据请求
NSString *urlStr = [NSStringstringWithFormat:@"http://172.16.150.156:8088/sign/findCardSignListByTeacher.do?teacherId=%@",[DataManagersharedInstance].userInfo.userId];
NSURL *url = [NSURLURLWithString:urlStr];
// 2. Request
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
// 3. Connection
[NSURLConnectionsendAsynchronousRequest:request queue:[[NSOperationQueuealloc] init]completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {
if (connectionError ==nil) {
// 网络请求结束之后执行!
// 将Data转换成字符串
NSString *str = [[NSStringalloc] initWithData:dataencoding:NSUTF8StringEncoding];
// num = 2
NSLog(@"%@ %@", str, [NSThreadcurrentThread]);
}
}];