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];

}

 

posted on 2013-11-24 21:06  林源  阅读(405)  评论(0编辑  收藏  举报

导航