---------------------------------------NetworkReques.h文件
@interface MainConnection :NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
@property (nonatomic,strong)NSURLConnection *conn;
@property (nonatomic,strong)NSMutableData *webData;
@property (nonatomic,strong)NSString *parsType;
@property (nonatomic,assign)BOOL isFinished;
@property (nonatomic,assign)BOOL isSumit;
/**
* 发送get请求
*/
- (void)getSendUrl:(NSString *)urlStr;
/**
* 接口名称
* 接口参数
* 发送Post请求
*/
-(void)sendPostRequest:(NSString *)urls HttpBody:(NSString *)part;
----------------------------------------NetworkReques.m文件
-(void)getSendUrl:(NSString *)urlStr{
urlStr = [NSStringstringWithFormat:@"%@%@",UrlHead,urlStr];
NSURL *url = [NSURLURLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
[request setTimeoutInterval:10];
[request setHTTPMethod:@"GET"];
_conn = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
if (_conn) {
_webData = [NSMutableDatadata];
}
_isFinished =NO;
while (!_isFinished) {
[[NSRunLoopcurrentRunLoop]runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];
}
}
-(void)sendPostRequest:(NSString *)urls HttpBody:(NSString *)parm
{
//获取手机的imsi 46001则为联通
// NSString *str = [MyTools getIMSI];
//判断当前网络状况是否是wifi
// NSInteger stateNum = [IsNetWork isWIFI];
// if ([str isEqualToString:@"46001"] && stateNum == 2) {
// urls = [NSString stringWithFormat:@"%@%@",LianTongUrlHead,urls];
// }else{
urls = [NSStringstringWithFormat:@"%@%@",UrlHead,urls];
// }
NSURL *url = [NSURLURLWithString:urls];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
[request setTimeoutInterval:10];
[request setHTTPMethod:@"POST"];
request.HTTPBody = [parmdataUsingEncoding:NSUTF8StringEncoding];
_conn = [[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
if (_conn) {
_webData = [NSMutableDatadata];
}
_isFinished =NO;
while (!_isFinished) {
[[NSRunLoopcurrentRunLoop]runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];
}
}
#pragma mark - detegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_webData appendData:data];
WBLog(@"%@",_webData);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
WBLog(@"%@",error);
if (_isSumit == YES) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"MainConnection" object:nil userInfo:@{@"linkServer":@"PostLink"}];
}else{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MainConnection" object:nil userInfo:@{@"linkServer":@"GetLink"}];
}
[MyToolscreateMyAlertView:nilandTitle:@"无法连接到服务器,请求超时,稍后再试!"];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
_isFinished =YES;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_webDatasetLength:0];
}
调用
//全局参数
NetworkReques *post;
BOOL isSubmitBool ;
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClickSign:) name:@"MainConnection" object:nil];
}
-(void)onClickSign:(NSNotification *)notifier{
NSString *str = [notifier.userInfo objectForKey:@"linkServer"];
if ([str isEqualToString:@"PostLink"]) {
isSubmit = NO;
}
self.act.hidesWhenStopped = YES;
[self.act stopAnimating];
}
-(void)onClickSubmit{
if(isSubmitBool== YES){
return;
}
isSubmitBool==YES;
NSString *urlStr = [NSStringstringWithFormat:@"IOS_InsertTrip"];
NSString *parmStr = [NSStringstringWithFormat:kIOS_InsertTrip,
delegate.SellerCode,
dataSource[1],
dataSource[0],
dataSource[2],
dataSource[4],
mp3str1,
dataSource[3]];
[selfsendPostRequest:urlStr andHttpBody:parmStr andUrlType:@"tijiao"];
}
#pragma mark - SendPostRequest
-(void)sendPostRequest:(NSString *)url andHttpBody:(NSString *)parm andUrlType:(NSString *)urlType{
post = [[NetworkRequesalloc] init];
post.parsType = urlType;
[post sendPostRequest:url HttpBody:parm];
post.isSubmit = YES;
if (post.isFinished ==YES) {
if ([post.parsType isEqualToString:@"tijiao"]) {
[selfparesDataObject:urlType];
}
}else{
isSubmitBool =NO;
}
}
- (void)paresDataObject:(NSString *)urlType{
NSString *str = [[NSStringalloc] initWithData: post.webData encoding:NSUTF8StringEncoding];
NSArray *array = [strcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];
NSString *str2 = [arrayobjectAtIndex:4];
if ([urlTypeisEqualToString:@"tijiao"]) {
if ([str2isEqualToString:@"true"]) {
[MyToolscreateMyAlert:selfandTitle:@"提交成功!"];
}else{
isSubmitBool =NO;
[MyToolsMBProgressHUD:selfandContent:@"提交失败!"];
}
}
}