zoukankan      html  css  js  c++  java
  • AFHTTPRequestOperationManager网络请求的时候添加一个菊花,requestmapping

    问:

    Can you help me to understand, how to use UIActivityIndicatorView+AFNetworking or UIProgressView+AFNetworking. Do I need to create one more UIViewController and if yes, where do I need to implement it? have found out that I need to use setProgressWithUploadProgressOfOperation or setAnimatingWithStateOfOperation, but I still need an example.

    my code is:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:@"blablabla" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
        // 3
        //[self.view setAnimatingWithStateOfOperation:operation];
        NSLog(@"JSON: %@", responseObject);
        NSArray *carsList = responseObject[@"data"];
        [self showStream:carsList];
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

    答:

    I guess, you want to show UIActivityIndicator while operation is loading. So you have to do something like this:

    // your code
    
    AFHTTPRequestOperation *operation = [manager GET:@"blablabla" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // even more code
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // and here
    }];
    
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] init];
    indicatorView.frame = /*calculate frame here*/;
    [self.view addSubView:indicatorView];
    [indicatorView setAnimatingWithStateOfOperation:operation];

    ------------------------------------------------------------------------
    官方教程
    https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-2.0-Migration-Guide

    AFHTTPRequestOperation Example

    NSURL *URL = [NSURL URLWithString:@"http://example.com/foo.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", responseObject);
    } failure:nil];
    [operation start];

    AFHTTPRequestOperationManager Example

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:@"http://example.com/foo.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", responseObject);
    } failure:nil];
    -------------------------------------------------------

    AFHTTPRequestOperationManager注意点


    ActionSheet在控制器view上,因为点击拍照时,会modal出另外一个控制器,当前的控制器view就会从窗口中移除。就会报ActionSheet不在窗口上。
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"图片上传" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册",nil];
        [sheet showInView:self.view];
    }

    #pragma mark - 实现UIActionSheetDelegate代理方法
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
        ipc.delegate = self;
        switch (buttonIndex) {
            case 0:
                if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
                ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            case 1:
                if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
                ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
               
            default:
                break;
        }
        [self presentViewController:ipc animated:YES completion:nil];
    }
    注意:
     
    如果在switchcase中定义一个局部变量,需要用大括号括住。

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        // 关闭UIImagePickerController控制器
        [self dismissViewControllerAnimated:YES completion:nil];
        NSLog(@"%@",info);
        // 获取图片
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        self.imageV.image = image;
    }
    - (IBAction)upLoad
    {
        // 创建一个管理者
        AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
        // 设置参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"username"] = @"123";
        params[@"pwd"] = @"123";
       
        // 设置URL
        NSString *url = @"http://192.168.15.56:8080/MJServer/upload";
        [manger POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
              
              注意:
              在发送请求之前会自动调用这个block
            需要在这个block中添加文件参数到formData
            NSData *fileData = UIImageJPEGRepresentation(self.imageV.image, 1.0);
            [formData appendPartWithFileData:fileData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"上传成功");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"上传失败");
        }];
    }

    注意:
    1.保留照片的质量
    NSData *fileData = UIImageJPEGRepresentation(self.imageV.image1.0);

    2.AFN不能监听文件的上传进度,ASI可以监听文件的上传进度。

    3.需要上传的文件的具体数据
    FileData : 需要上传的文件的具体数据
    name : 服务器那边接收文件用的参数名
    fileName : (告诉服务器)所上传文件的文件名
    mimeType : 所上传文件的文件类型
    NSData *fileData = UIImageJPEGRepresentation(self.imageV.image, 1.0);
    [formData appendPartWithFileData:fileData name:@"file" fileName:@"image.png" mimeType:@"image/png"];

    4.需要上传的文件的URL路径
    FileURL : 需要上传的文件的URL路径
    name : 服务器那边接收文件用的参数名
    fileName : (告诉服务器)所上传文件的文件名
    mimeType : 所上传文件的文件类型
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"itcast" withExtension:@"txt"];
    [formData appendPartWithFileURL:url name:@"file" fileName:@"test.txt" mimeType:@"text/plain" error:nil];





  • 相关阅读:
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (四) 自动化部署
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (三) 服务观测
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (二) 部署微服务程序
    阿里云 k8s 部署 Spring Cloud Alibaba 微服务实践 (一) 部署 Nacos
    C++知识点
    libmkl 学习笔记
    基于tesseract-OCR进行中文识别
    poco编译与运行
    Linux下的I/O复用与epoll详解(转载)
    高并发网络编程之epoll详解(转载)
  • 原文地址:https://www.cnblogs.com/allanliu/p/4223806.html
Copyright © 2011-2022 走看看