AFNetworking的简单使用

2019-06-06  本文已影响0人  小石头呢

iOS的info.plist文件配置

一.使用AFNetworking发送Get,Post请求

Get请求

1.创建管理者

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

2.设置接受的类型

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"text/plain",@"image/gif", nil];

3.get请求路径

NSString *urlString = @"http://127.0.0.1/AF_Hello_Get.php?name=jack&password=123";

4.发起请求

[manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"成功:%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"失败:%@",error);
}];

AF_Hello_Get.php里面的内容

<?PHP
   #获取用户输入的姓名和密码
   $name = $_GET["name"];
   $pwd = $_GET["password"];

    // $name = $_POST["name"];
    // $pwd = $_POST["password"];
    
    $user = array(
             "name"=>$name,
             "password"=>$pwd,
    );


   $result = array(
              "user"=>$user,
              "total"=>"2",
              "status"=>0,
   );

    header('Content-Type:application/json');
    echo json_encode($result);
?>

Post

1.post请求路径以及传递的参数

urlString = @"http://127.0.0.1/AF_Hello_Post.php";
    NSDictionary *param = @{@"name":@"jack",@"password":@"123"};

2.发起Post请求

[manager POST:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"成功:%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"失败:%@",error);
}];

AF_Hello_Post.php里面的内容

<?PHP
   #获取用户输入的姓名和密码
   $name = $_POST["name"];
   $pwd = $_POST["password"];
    
   $user = array(
             "name"=>$name,
             "password"=>$pwd,
    );


   $result = array(
              "user"=>$user,
              "total"=>"2",
              "status"=>0,
   );

    header('Content-Type:application/json');
    echo json_encode($result);
?>

二.使用AFNetworking上传图片或者视频

1.post请求路径

urlString = @"http://127.0.0.1/AF_UpLoadFile.php";

2.传递的参数

param = @{@"name":@"jack",@"password":@"123"};

3.上传的文件URL

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test.png" ofType:nil];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

4.发起Post请求

[manager POST:urlString parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //追加信息
        [formData appendPartWithFileURL:fileURL name:@"file" fileName:@"test.png" mimeType:@"image/png" error:nil];

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"上传进度:%f",uploadProgress.fractionCompleted);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功:%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败:%@",error);
}];

AF_UpLoadFile.php的内容

<?PHP

   $name = $_POST["name"];
   $pwd = $_POST["password"];

    //获取文件
    $file = $_FILES["file"];
    
    header('Content-Type:application/json');

    //获取文件信息
    if($file["error"] > 0){
        //读取文件出错
//        header('Content-Type:application/json');
//
        $result = array(
                      "error"=>$file["error"],
                      );

        echo json_encode($result);
        
    }else{
        //输出详细信息
        // echo "上传的文件名:".$file["name"]."<br/>";
        // echo "上传的文件类型:".$file["type"]."<br/>";
        // echo "上传的文件大小:".($file["size"]/1024)."Kb<br/>";
        // echo "临时路径:".$file["tmp_name"]."<br/>";
        
        //判断文件类型
        $type = $file["type"];
        $path;
        if($type == "image/jpeg" || $type == "image/png"){
            //图片
            $path = "upLoad/img/";
        }else if($type == "video/mp4"){
            //视频
            $path = "upLoad/video/";
        }
    }
    
    $filePath = $path.$file["name"];
    
    //判断文件是否存在
    if(file_exists($filePath)){
        //存在
        
        $result = array(
                      "error"=>$file["error"],
                      );

        echo json_encode($result);
    }else{
        //不存在
        //将临时文件里面的文件移动到指定目录
        move_uploaded_file($file["tmp_name"],$filePath);

        $result = array(
                      "name"=>$name,
                      "pwd"=>$pwd,
                      "file_name"=>$file["name"],
                      "file_Path"=>$filePath,
                      );

        echo json_encode($result);
    }
?>

demo链接
链接:https://pan.baidu.com/s/1SO2FT-S2J9993R8xUp8l9w 密码:g2e3

上一篇下一篇

猜你喜欢

热点阅读