• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

分享一个自己用的Objective-C的Http接连类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

很久没有更新博客了,所以分享一个。

@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

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap