zoukankan      html  css  js  c++  java
  • ISO中AFN的使用步骤

    1.依赖的框架

    * MobileCoreServices.framework

    * SystemConfiguration.framework

    * Security.framework

    2.主头文件:AFNetworking.h

    3.创建POST请求对象

    // BaseURL是基准路径,格式为-> 协议头://主机名,不能包含其他路径

    AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://api.weibo.com"]];

    // Method是请求方法

    // path是拼接在基准路径后面的请求路径

    // parametersPOST请求的参数

    NSURLRequest *post = [client requestWithMethod:@"POST" path:@"oauth2/access_token" parameters:@{

                          @"client_id" : kAppKey,

                          @"client_secret" : kAppSecret,

                          @"grant_type" : @"authorization_code",

                          @"redirect_uri" : kRedirectURI,

                          @"code" : requestToken

    }];

    4.发送请求,解析服务器的JSON数据

    // 创建操作对象

    NSOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:post

    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        // AFN认为请求成功会调用

    }

    failure : ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        // AFN认为请求失败会调用

    }];

    // 执行操作

    [op start];

    5.AFJSONRequestOperation默认不接收text/plain类型的数据,当服务器返回text/plain类型的数据时,会认为出错了。可以通过修改源代码解决问题

    * 修改AFJSONRequestOperation的下列方法

    + (NSSet *)acceptableContentTypes {

        return [NSSet setWithObjects:@"text/plain", @"application/json", @"text/json", @"text/javascript", nil];

    }

  • 相关阅读:
    为什么要配置PATH环境变量?如何配置
    JDK,JRE,JVM三者之间的关系,以及JDK、JRE包含的主要结构有哪些
    常用的开发工具
    API文档说明
    Java注释(Comment)
    EditPlus和notepad++配置
    Java第一个程序--HelloWorld
    cmd命令和快捷键
    如何以计算机的方式去思考
    藤野先生后来怎么样了?被自卑隔阂的友谊
  • 原文地址:https://www.cnblogs.com/changxs/p/3440491.html
Copyright © 2011-2022 走看看