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(@"請求失敗");
}];