AFNetWorking对网络数据post请求获取数据步骤
1.创建“AFHTTPRequestOperationManager”对象;
2.设置请求参数array;
3.发送请求:
[managerPOST:url parameters:array success:^(AFHTTPRequestOperation*operation,idresponseObject) {
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
}];
例子:
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManagermanager];
NSDictionary*dict = @{@"name":@"zhangsan"};
NSDictionary*dict1 = @{@"name":@"wangwu"};
NSArray*array = @[dict,dict1];
//设置请求格式
manager.requestSerializer = [AFJSONRequestSerializerserializer];
//设置返回格式
manager.responseSerializer = [AFHTTPResponseSerializerserializer];
[managerPOST:@"http://localhost/postjson.php"parameters:array success:^(AFHTTPRequestOperation*operation,idresponseObject) {
NSString*result = [[NSStringalloc]initWithData:responseObjectencoding:NSUTF8StringEncoding];
NSLog(@"%@", result);
}failure:^(AFHTTPRequestOperation*operation,NSError*error) {
}];
AFHTTPRequestOperationManager的具体使用
1.创建管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"哈哈";
params[@"pwd"] = @"123";
3.发送请求
NSString *url = @"http://localhost:8080/LWServer/login";
[mgr POST:url parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// 请求成功的时候调用这个block
NSLog(@"请求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// 请求失败的时候调用调用这个block
NSLog(@"请求失败");
}];
// GET请求
[mgr GET:url parameters:params
success:^(AFHTTPRequestOperation \*operation, id responseObject) {
// 请求成功的时候调用这个block
NSLog(@"请求成功---%@", responseObject);
} failure:^(AFHTTPRequestOperation \*operation, NSError *error) {
// 请求失败的时候调用调用这个block
NSLog(@"请求失败");
}];