AFNetWorking封装请求数据

//

//  YXYAppDelegate.m

//  AFNetWorking封装

//

//  Created by lanjiying on 14/11/20.

//  Copyright (c) 2014年 lanjiying. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "AFNetworking.h"

typedef
void(^CompletionLoad)(NSObject *result);

@interface YXYDataService :
NSObject

///如果请求中没有请求头,使用此方法,如果需要传图片则需要将图片封装成NSDate,装到params中,url为地址,params为请求体没有传nil,get和post方法可以忽略大小写

+(AFHTTPRequestOperation *)requestWithURL:(NSString *)url  params:(NSMutableDictionary
*)params httpMethod:(NSString *)httpMethod block:(CompletionLoad)block;

///url为请求地址,params是请求体,传字典进去,,httpMethod
是请求方式,block是请求完成做得工作,header是请求头,也是传字典过去(发送请求获得json数据),如果没有则传nil,如果只有value而没有key,则key可以设置为anykey(但是此方法暂时没设置传图片)

+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url

requestHeader:(NSDictionary *)header

params:(NSMutableDictionary *)params

httpMethod:(NSString *)httpMethod

block:(CompletionLoad)block

;

//

//  YXYAppDelegate.m

//  AFNetWorking封装

//

//  Created by liujianjian on 14/11/20.

//  Copyright (c) 2014年 lanjiying. All rights reserved.

//

#import "YXYDataService.h"

@implementation YXYDataService

//

+(AFHTTPRequestOperation *)requestWithURL:(NSString *)url  params:(NSMutableDictionary
*)params httpMethod:(NSString *)httpMethod block:(CompletionLoad)block

{

//创建request请求管理对象

AFHTTPRequestOperationManager * manager =[AFHTTPRequestOperationManager
manager];

AFHTTPRequestOperation * operation =
nil;

//GET请求

NSComparisonResult comparison1 = [httpMethod
caseInsensitiveCompare:@"GET"];

if (comparison1 ==
NSOrderedSame) {

operation =[manager
GET:url parameters:params
success:^(AFHTTPRequestOperation *operation,
id responseObject) {

block(responseObject);

}
failure:^(AFHTTPRequestOperation *operation,
NSError *error) {

block(error);

}];

}

//POST请求

NSComparisonResult comparisonResult2 = [httpMethod
caseInsensitiveCompare:@"POST"];

if (comparisonResult2 ==
NSOrderedSame)

{

//标示

BOOL isFile = NO;

for (NSString * key
in params.allKeys)

{

id value = params[key];

//判断请求参数是否是文件数据

if ([value isKindOfClass:[NSData
class]]) {

isFile =
YES;

break;

}

}

if (!isFile) {

//参数中没有文件,则使用简单的post请求

operation =[manager
POST:url

parameters:params

success:^(AFHTTPRequestOperation *operation,
id responseObject) {

if (block != nil) {

block(responseObject);

}

}
failure:^(AFHTTPRequestOperation *operation,
NSError *error) {

if (block != nil) {

//

}

}];

}else

{

operation =[manager
POST:url

parameters:params

constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

for (NSString *key
in params) {

id value = params[key];

if ([value isKindOfClass:[NSData
class]]) {

[formData
appendPartWithFileData:value

name:key

fileName:key

mimeType:@"image/jpeg"];

}

}

}
success:^(AFHTTPRequestOperation *operation,
id responseObject) {

block(responseObject);

}
failure:^(AFHTTPRequestOperation *operation,
NSError *error) {

NSLog(@"请求网络失败");

}];

}

}

//设置返回数据的解析方式

operation.responseSerializer =[AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingMutableContainers];

return operation;

}

///url为请求地址,params是请求体,传字典进去,,httpMethod
是请求方式,block是请求完成做得工作,header是请求头,也是传字典过去(发送请求获得json数据),如果没有则传nil,如果只有value而没有key,则key可以设置为anykey

+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url

requestHeader:(NSDictionary *)header

params:(NSMutableDictionary *)params

httpMethod:(NSString *)httpMethod

block:(CompletionLoad)block

{

NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL
URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];

//添加请求头

for (NSString *key
in header.allKeys) {

[request
addValue:header[key]
forHTTPHeaderField:key];

}

//get请求

NSComparisonResult compResult1 =[httpMethod
caseInsensitiveCompare:@"GET"];

if (compResult1 ==
NSOrderedSame) {

[request
setHTTPMethod:@"GET"];

if(params != nil)

{

//添加参数,将参数拼接在url后面

NSMutableString *paramsString = [NSMutableString
string];

NSArray *allkeys = [params
allKeys];

for (NSString *key
in allkeys) {

NSString *value = [params
objectForKey:key];

[paramsString
appendFormat:@"&%@=%@", key, value];

}

if (paramsString.length >
0) {

[paramsString replaceCharactersInRange:NSMakeRange(0,
1) withString:@"?"];

//重新设置url

[request
setURL:[NSURL
URLWithString:[url
stringByAppendingString:paramsString]]];

}

}

}

//post请求

NSComparisonResult compResult2 = [httpMethod
caseInsensitiveCompare:@"POST"];

if (compResult2 ==
NSOrderedSame) {

[request
setHTTPMethod:@"POST"];

for (NSString *key
in params) {

[request
setHTTPBody:params[key]];

}

}

//发送请求

AFHTTPRequestOperation *requstOperation = [[AFHTTPRequestOperation
alloc]
initWithRequest:request];

//设置返回数据的解析方式(这里暂时只设置了json解析)

requstOperation.responseSerializer = [AFJSONResponseSerializer
serializer];

[requstOperation
setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
id responseObject) {

if (block != nil) {

block(responseObject);

}

}
failure:^(AFHTTPRequestOperation *operation,
NSError *error) {

NSLog(@"Error: %@", [error
localizedDescription]);

if (block != nil) {

block(error);

}

}];

[requstOperation
start];

return requstOperation;

}

@end

时间: 2024-10-12 13:38:08

AFNetWorking封装请求数据的相关文章

AFNetworking网络请求数据

//创建AFNetworking的请求操作    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList.php?class_id=%ld&pag

BeanUtils--02--- 创建工具类 封装request 请求数据到实体类中

1. 前台传过来的数据都是字符串, 基本类型BeanUtils会自动转换, 日期类型需要注册转换器,自带的DateLocalConvert转换器未实现空字符串""的判断 public static <T> T copy2Bean(HttpServletRequest request, Class<T> clazz) { ConvertUtils.register(new Converter() {//注册日期转换器 public Object convert(C

Struts2请求数据自动封装和数据类型转换

方式1:jsp表单数据填充到action中的属性: 方式2:jsp表单数据填充到action的对象的属性: 方式1: 第一步:引包,省去 第二步:配置struts2的过滤器 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmln

AFNetworking请求数据总是返回错误的修改

最近我看到很多AFNet的教程,我就在想怎么那么多人不明白怎么用AFNet.很多人回答的问题都是一样的怎么还有人不会用.我估计很多人都遇到了这个错误 我运行了 这段代码 AFHTTPRequestOperationManager  * manager  =  [ AFHTTPRequestOperationManager  manager ]; [manager GET:@"http://211.154.151.249:8866/ogPortal/getCarType.do"param

struts中的请求数据自动封装

Struts 2框架会将表单的参数以同名的方式设置给对应Action的属性中.该工作主要是由Parameters拦截器做的.而该拦截器中已经自动的实现了String到基本数据类型之间的转换工作.在struts中,默认使用拦截器 <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/> 进行请求数据自动封装,它会JSP中提交的

原生与jQuery封装的ajax请求数据及状态码

原生Ajax 请求数据 btn.addEventListener('click',function(){ if(window.XMLHttpRequest){ var xhr = new window.XMLHttpRequest(); }else{ //兼容IE浏览器 var xhr = new ActiveXObject('Microsoft.XMLHTTP'); } //请求方式get/post //请求URL //ture 异步请求:false 同步请求 xhr.open('get','

AFNetworking application/x-www-form-urlencoded 数据请求方式

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p4 { margin: 0.0px 0.0px 0.0px 0.

iOS开发 - AFNetworking网络请求

AFNetworking 什么是AFN 全称是AFNetworking,是对NSURLConnection.NSURLSession的一层封装 虽然运行效率没有ASI高,但是使用比ASI简单 在iOS开发中,使用比较广泛 AFN的github地址 https://github.com/AFNetworking/AFNetworking AFHTTPRequestOperationManager 是AFN中最重要的对象之一 封装了HTTP请求的常见处理 GET\POST请求 解析服务器的响应数据

捕获异常、存sd卡、封装请求头体、加密map值、网络工具类、生成Json、Https协议、传log日志到服务器、app崩溃友好重启

点击打开链接,免积分下载 在集成了统计SDK(友盟统计,百度统计等)之后,有一个非常有利于测试的功能:错误分析!此功能能够将程序在运行中碰到的崩溃(runtimeException)问题反馈到服务器,帮助开发者改善产品,多适配机器.然而在公司android开发中不集成这些SDK,那应该怎么实现这样的功能呢?下面让我们来看下如何使用UncaughtExceptionHandler来捕获异常. 在Android开发中,常常会出现uncheched Exception 导致程序的crash,为了提供良