数据请求,分为Post与Get方式;Post安全性相对高,速度慢,对于向服务器请求“拿”数据的一般用Get方式,往服务器写数据的用Post。
/*
//get 方式
NSString *properlyEscapedURL = [test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:properlyEscapedURL];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
// operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
NSString *requestTmp = [NSString stringWithString:operation.responseString];
NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
//系统自带JSON解析
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"resultDic:%@", resultDic);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@",error);
}];
[operation start];
*/
#define test @"http://192.168.1.222/qiuyou/qiuyouapi/qiuyouv4/api/index.php?m=addteam"
//post方式请求;
// &userid=51&teamname=创建球队_11
// reqString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
NSDictionary *parameters = @{@"userid":@"51", @"teamid":@"创建球队13"};
[manager POST:test parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"%@", responseObject);
NSString *requestTmp = [NSString stringWithString:operation.responseString];
NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
//系统自带JSON解析
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"resultDic:%@", resultDic);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"-----------错误信息:%@", error);
}];
// AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
// NSDictionary *parameters = @{@"cityid": @"12"};
//
// [manager POST:test parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"JSON: %@", responseObject);
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// NSLog(@"Error: %@", error);
// }];