プロジェクトにおけるAFNの使い方
8386 ワード
1.ネットワークの状態を監視する
2.POST要求の送信
3.GETリクエストの送信
4.画像をアップロード
5.AFHTTPSessionManager内部にはNSURLSession AFHTTPRequestOperationManager内部にはNSURLConnection内部には両方ともネットワークリクエストを送信できます.6.ダウンロード
7.画像をアップロードする別の方法(パラメータなし)
8.xmlのデータを解析する
9.戻りデータの解析
// 1.
AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
// 2.
[mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
// , block
switch (status) {
case AFNetworkReachabilityStatusUnknown: //
[self setupRefresh];
break;
case AFNetworkReachabilityStatusNotReachable: // ( )
[SVProgressHUD showImage:nil status:@" "];
[self getStatusFromCache];
break;
case AFNetworkReachabilityStatusReachableViaWWAN: //
[self setupRefresh];
break;
case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
[self setupRefresh];
break;
}
}];
// 3.
[mgr startMonitoring];
2.POST要求の送信
//
NSMutableDictionary *params = [NSMutableDictionary dictionary];
TWAccount *accout = [TWAccountTool account];
params[@"key"] = accout.key;
params[@"client"] = @"ios";
[[AFHTTPSessionManager manager] POST:@"http://www.jixuejiyong.com/mobile/index.php?act=hg_member_sns_home&op=friends_circle" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
TTLog(@"%@",responseObject);
self.topics = [TWMyStatus mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"tracelist"]];
//
[TWStatusCacheTool addStatuses:_topics];
//------------ UI -----------
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
//-----------------------------------------------------
[self.tableView.mj_header endRefreshing];
[SVProgressHUD dismiss];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[self.tableView.mj_header endRefreshing];
[SVProgressHUD showErrorWithStatus:@" "];
}];
}
3.GETリクエストの送信
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"type"] = @1;
params[@"phone"] = self.phone;
params[@"captcha"] = self.param1[@"datas"][@"captcha"];
params[@"nchash"] = self.param1[@"datas"][@"nchash"];
[[AFHTTPSessionManager manager] GET:@"http://www.jixuejiyong.com/mobile/index.php?act=hg_member_sns_home&op=get_captcha" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (![responseObject[@"datas"][@"result"] isEqualToString:@"ture"]) {
[SVProgressHUD showErrorWithStatus:@" "];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@" "];
}];
4.画像をアップロード
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//
UIImage *image = info[@"UIImagePickerControllerEditedImage"];
//
self.imgView.image = [ImageTool image:image byScalingToSize:CGSizeMake(120, 120)];
//
NSString *patch = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSData *imageData = UIImagePNGRepresentation(self.imgView.image);
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@",patch,@"icon.png"] atomically:YES];
//
NSMutableDictionary *params = [NSMutableDictionary dictionary];
TWAccount *accout = [TWAccountTool account];
params[@"key"] = accout.key;
params[@"client"] = @"ios";
//
[[AFHTTPSessionManager manager] POST:@"http://www.jixuejiyong.com/mobile/index.php?act=hg_member_sns_home&op=changeAvatarHandle" parameters:params constructingBodyWithBlock:^(id _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"icon.png" mimeType:@"image/png"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
TWAccount *accout = [TWAccountTool account];
accout.member_avatar = responseObject[@"datas"][@"result"][@"src"];
[TWAccountTool saveAccount:accout];
TTLog(@" %@",responseObject[@"datas"][@"result"][@"src"]);
if ([responseObject[@"datas"][@"src"] isEqualToString:@""]) {
[SVProgressHUD showErrorWithStatus:@" "];
}else{
//
NSMutableDictionary *params1 = [NSMutableDictionary dictionary];
params1[@"key"] = accout.key;
params1[@"client"] = @"ios";
params1[@"id"] = accout.member_id;
params1[@"src"] = responseObject[@"datas"][@"result"][@"src"];
//
[[AFHTTPSessionManager manager] POST:@"http://www.jixuejiyong.com/mobile/index.php?act=hg_member_sns_home&op=avatarheader" parameters:params1 progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject[@"datas"][@"result"] isEqualToString:@"success"]) {
[SVProgressHUD showImage:nil status:@" "];
}else{
[SVProgressHUD showErrorWithStatus:@" "];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@" "];
}];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[SVProgressHUD showErrorWithStatus:@" "];
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}
5.AFHTTPSessionManager内部にはNSURLSession AFHTTPRequestOperationManager内部にはNSURLConnection内部には両方ともネットワークリクエストを送信できます.6.ダウンロード
- (void)download
{
// AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
//
// [mgr downloadTaskWithRequest: progress: destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
// return
// } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
//
// }];
}
7.画像をアップロードする別の方法(パラメータなし)
- (void)upload2
{
// AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// [mgr uploadTaskWithRequest: fromData: progress: completionHandler:];
}
8.xmlのデータを解析する
- (void)xml
{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// responseSerializer
// AFN, XML
mgr.responseSerializer = [AFXMLParserResponseSerializer serializer];
NSDictionary *params = @{
@"username" : @"520it",
@"pwd" : @"520it",
@"type" : @"XML"
};
[mgr GET:@"http://120.25.226.186:32812/login" parameters:params success:^(NSURLSessionDataTask *task, NSXMLParser *parser) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@" ---%@", error);
}];
}
9.戻りデータの解析
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
// “ ”,
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
// [AFJSONResponseSerializer serializer]; // JSON
// [AFXMLParserResponseSerializer serializer]; // XML