分享一个自己用的Objective-C的Http接连类
很久没有更新博客了,所以分享一个。
@protocol HttpListenerDelegate; @interface BaseHttp : NSObject
{
} @property (nonatomic, weak) id<HttpListenerDelegate> delegate; @property (nonatomic, M_STRONG) NSURLConnection *connect;
@property (nonatomic, M_STRONG) NSMutableData *receiveData; @property (nonatomic, M_STRONG) NSString *httpUrl;
//设置当前服务的唯一标示,默认为当前的URL
@property (nonatomic, M_STRONG) NSString *identify; - (id)initWithHttpUrl:(NSString *)url; //开始调用远程服务
- (void)execute;
- (void)execute:(id)param;
//接收到服务器回应的时候调用此方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
//数据传完之后调用此方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
//网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error; @end @protocol HttpListenerDelegate <NSObject> @optional
//接收到服务器回应的时候调用此方法
- (void)didReceiveResponse:(NSURLResponse *)response identify:(NSString *)identify; - (void)didReceiveData:(NSData *)data identify:(NSString *)identify;
//后台加载数据完成
- (void)didFinishLoading:(NSMutableData*)receiveData identify:(NSString *)identify;
//网络请求异常
- (void)didFailWithError:(NSError *)error identify:(NSString *)identify; @end
//
// BaseHttp.m
// myb-ios
//
// Created by warrior gao on 13-6-7.
// Copyright (c) 2013年 51myb. All rights reserved.
// #import "BaseHttp.h" @implementation BaseHttp
- (id)initWithHttpUrl:(NSString *)url
{
self = [self init];
_httpUrl = [NSString stringWithFormat: @"%@%@",SERVER_URL, url];
_identify = url;
return self;
} -(void)setHttpUrl:(NSString *)httpUrl
{
_httpUrl = httpUrl;
if(!(_identify))
_identify = httpUrl;
} //开始调用远程服务
- (void)execute
{
[self execute:@""];
} - (void)execute:(id)param
{
if(DEBUG) {
NSLog(@"开始请求:%@", _httpUrl);
} //第一步,创建URL
NSURL *url = [NSURL URLWithString:_httpUrl];
//第二步,创建请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:DEFAULT_HTTP_TIMEOUT];
[request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:HTTP_HEADER_VALUE forHTTPHeaderField:HTTP_HEADER_KEY]; NSData *bodyData = nil;
if([param isKindOfClass:[NSString class]]){
bodyData = [param dataUsingEncoding:NSUTF8StringEncoding];
} else if ([param isKindOfClass:[NSData class]]){
bodyData = param;
} else if ([param isKindOfClass:[NSNumber class]]) {
bodyData = [[param stringValue] dataUsingEncoding:NSUTF8StringEncoding];
} [request setHTTPBody:bodyData]; //第三步,连接服务器 _connect = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(_connect){
_receiveData = [NSMutableData data];
}
} //接收到服务器回应的时候调用此方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_receiveData setLength:0];
if([_delegate respondsToSelector:@selector(didReceiveResponse:identify:)])
[_delegate didReceiveResponse:response identify:_identify];
}
//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receiveData appendData:data];
if([_delegate respondsToSelector:@selector(didReceiveData:identify:)])
[_delegate didReceiveData:data identify:_identify];
}
//数据传完之后调用此方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(DEBUG){
NSLog(@"请求后台数据完成---:%@",_identify);
} if(DEBUG){
NSString *receiveStr = [[NSString alloc] initWithData:_receiveData encoding:NSUTF8StringEncoding];
NSLog(@"%@",receiveStr);
} if([_delegate respondsToSelector:@selector(didFinishLoading:identify:)])
[_delegate didFinishLoading:_receiveData identify:_identify];
}
//网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
if(DEBUG){
NSLog(@"%@",[error localizedDescription]);
} if([_delegate respondsToSelector:@selector(didFailWithError:identify:)])
[_delegate didFailWithError:error identify:_identify];
else {
[AlertViewHelper alertMessage:HTTP_CONNECT_ERROR];
}
} @end
分享一个自己用的Objective-C的Http接连类的更多相关文章
- 分享一个手机端好用的jquery ajax分页类
分享一个手机端好用的jquery ajax分页类 jquery-ias.min.js 1,引入jquery-ias.min.js 2,调用ajax分页 <script type="te ...
- 分享一个简单的C#的通用DbHelper类(支持数据连接池)
每次新项目的时候,都要从头去找一遍数据库工具类.这里分享一个简单实用的C#的通用DbHelper工具类,支持数据连接池. 连接池配置 <connectionStrings> <add ...
- 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...
- 分享一个MySQL分库分表备份脚本(原)
分享一个MySQL分库备份脚本(原) 开发思路: 1.路径:规定备份到什么位置,把路径(先判断是否存在,不存在创建一个目录)先定义好,我的路径:/mysql/backup,每个备份用压缩提升效率,带上 ...
- 分享一个与ABP配套使用的代码生成器源码
点这里进入ABP系列文章总目录 分享一个与ABP配套使用的代码生成器源码 真对不起关注我博客的朋友, 因最近工作很忙, 很久没有更新博客了.以前答应把自用的代码生成器源码共享出来, 也一直没有时间整理 ...
- 分享一个常用Adb命令
分享一个常用Adb命令 首先 首先感谢@xuxu的常用adb命令,收益良多,但是已经不能满足于我,所以补充了下. 再者 好久没发帖了,最近论坛老司机们都在讨论/总结,我就用这个干货回报吧. 最后 基于 ...
- 福利到~分享一个基于jquery的智能提示控件intellSeach.js
一.需求 我们经常会遇到[站内搜索]的需求,为了提高用户体验,我们希望能做到像百度那样的即时智能提示.例如:某公司人事管理系统,想搜索李XX,只要输入“李”,系统自然会提示一些姓李的员工,这样方便用户 ...
- 分享一个oraclehelper
分享一个拿即用的oraclehelper 首先要引用本机中的oralce access,如果是64位的话,也必须是64位运行,不然会报连接为空connection 等于null. using Orac ...
- 分享一个ruby网站 | 菜鸟教程
http://www.runoob.com/ruby/ruby-tutorial.html 分享一个ruby网站.
- 分享一个批量导出当前实例下的所有linkedserver脚本
分享一个批量导出当前实例下的所有linkedserver脚本 很多时候,我们都需要导出实例下面的登录用户,job,linkedserver等等 导出job比较复杂,下午写了一个脚本把所有的linked ...
随机推荐
- java.lang.NoSuchFieldError: VERSION_2_3_0 报错解决方案
java.lang.NoSuchFieldError: VERSION_2_3_0 at org.apache.struts2.views.freemarker.FreemarkerManager.c ...
- Object窥探
/* * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...
- What a version number means
http://stackoverflow.com/questions/3768261/best-practices-guidance-for-maintaining-assembly-version- ...
- leetcode:Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- oracle数据导入的常用命令
oracle 中数据库完全导入导出:cmd命令行模式 oracle数据库cmdfile数据库服务器constraints Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.ex ...
- 用FireMonkey写QQ皮肤
这是运行在Windows平台的效果,同样不需要改一行代码就可以运行在Mac Os,并且效果完全相同: 用FireMonkey做界面速度非常快,其提供的Effect ,Filter,Animation等 ...
- linux 2.6up的设备和设备驱动模型
在linux2.6 的设备和设备驱动模型构架中,所有的外部设备和驱动程序都挂在总线上 ,总线分为(usb -- USB设备,PCI -- PCI 设备 platform -- 直接和处理器进 ...
- 51nod1476 括号序列的最小代价
这题应该可以用费用流写吧?不过我想不出贪心来TAT.其实还是单调队列乱搞啊T_T //ÍøÉϵÄ̰ÐÄËã·¨ºÃÉñ°¡¡£¡£¡£ÎÒÖ»»áÓÃ×îС·ÑÓÃ×î´óÁ÷ÅÜTAT #in ...
- 转:asmx迷10分钟升级成wcf熟手指南
前言:本文旨在帮助从未接触过wcf(.svc文件)的webservice开发人员,快速将传统的webService/asmx技术迁移到wcf.高手就不用浪费时间往下看了:) 以下所有操作均为vs201 ...
- UIScrollView 期本使用方法
UIScrollView 1. contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量. // 设置scrollView的滚动偏移量 scrollView ...