zoukankan      html  css  js  c++  java
  • AFNetworking2.0简易GET,POST请求封装以及使用

    AFNetworking2.0简易GET,POST请求封装以及使用

    AFNetworking不用我赘述其强大性,本人仅仅做了非常简易的封装,解决了有时候请求出错的问题,提供源码给大家。

    封装源码库下载地址:

    http://pan.baidu.com/s/1wDLIQ

    源码:

    Networking.h 与 Networking.m

    //
    //  Networking.h
    //  Weather
    //
    //  Created by YouXianMing on 15/01/01.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "AFNetworking.h"
    
    @interface Networking : NSObject
    
    #pragma mark - GET请求
    + (void)GET:(NSString *)urlString
     parameters:(id)parameters
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
    
    #pragma mark - POST请求
    + (void)POST:(NSString *)urlString
      parameters:(id)parameters
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
    
    @end
    //
    //  Networking.m
    //  Weather
    //
    //  Created by YouXianMing on 15/01/01.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "Networking.h"
    
    @implementation Networking
    
    + (void)GET:(NSString *)urlString
     parameters:(id)parameters
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // 防止解析不出来
        manager.responseSerializer.acceptableContentTypes = 
            [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        [manager GET:urlString
          parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 if (success) {
                     success(operation, responseObject);
                 }
             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 if (failure) {
                     failure(operation, error);
                 }
             }];
    }
    
    + (void)POST:(NSString *)urlString
      parameters:(id)parameters
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // 防止解析不出来
        manager.responseSerializer.acceptableContentTypes = 
        [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
        [manager POST:urlString
           parameters:parameters
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  if (success) {
                      success(operation, responseObject);
                  }
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  if (failure) {
                      failure(operation, error);
                  }
              }];
    }
    
    @end

    GET请求带参数与不带参数之间的转换:

    POST请求:

    源码修改的地方:

  • 相关阅读:
    v-bind绑定属性
    vue 第二次学习笔记 v-once v-html
    P4428-[BJOI2018]二进制【树状数组,set】
    P5180-[模板]支配树
    《架构之美》阅读笔记一
    Python基础04----条件控制
    Tensorflow2.0笔记33——Keras 来搭建神经网络的“八股”套路
    每周总结
    架构漫谈阅读笔记01
    Tensorflow2.0笔记32——卷积神经网络
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4197880.html
Copyright © 2011-2022 走看看