网络请求

{ _request = [[ASIHTTPRequest alloc] initWithURL:[[NSURL alloc] initWithString:url]];
	_request.processType = processType; /* typedef enum{
	ProcessNone=0,//无
	ProcessDefault=1,//默认效果  黑色遮盖  
	ProcessSimple=2,//一般  只有转圈
} ProcessType;
 // 用来定义数据请求中界面的样式 */
	[_request setDelegate:self];
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
	
	//创建活动图标
	progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((self.navigationItem.titleView.frame.size.width-30)/2, (self.navigationItem.titleView.frame.size.height-60)/2, 20, 20)];
	progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
	[self.navigationItem.titleView addSubview:progress];
	[self _requestStart:processType];
	[_request setTimeOutSeconds:10*60];
	[_request startAsynchronous];
}

//开始请求
-(void) _requestStart:(int)processType{
	//无加载提示
	if (processType == ProcessNone) {
		return;
	}
	if (processType == ProcessDefault) {//有黑色的背景
		if (bvProgress == nil) {
			bvProgress = [[ButtonView alloc] initWithName:nil :@"加载中⋯⋯"];
			[bvProgress.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];   
			[bvProgress setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
			
			bvProgress.frame = CGRectMake(0, 0, [Global GetInstance].ScreenWidth, 480 - tabBarHeight);
			bvProgress.backgroundColor=BLACKBGCOLOR;
			//bvProgress.backgroundColor = [UIColor clearColor];
			progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((bvProgress.frame.size.width-30)/2, (bvProgress.frame.size.height-60)/2, 20, 20)];
			progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
			[bvProgress addSubview:progress];
			[progress startAnimating];
			[self.parentViewController.view addSubview:bvProgress];
		}
	}
	if (processType == ProcessSimple) {//一般的显示效果  只有转圈
		progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((320-30)/2, (460-44-tabBarHeight)/2, 20, 20)];
		progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
		[self.view addSubview:progress];
		[progress startAnimating];
	}
}


// 请求成功

- (void)requestFinished:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	//编码转换 gb2313 to UTF
	NSData * myResponseData = [request responseData];
	[self _requestSuccess:request.processType];
	//	NSLog
	NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingUTF8);//kCFStringEncodingUTF8
	NSString * myResponseStr = [[NSString alloc] initWithData:myResponseData encoding:enc];
	
    //JsonResult *result = nil;
    NSDictionary *dic = [myResponseStr JSONValue];
    if ([dic objectForKey:@"uid"]!=nil && ![[dic objectForKey:@"uid"] isEqualToString:@""]) {
        [UserInfo GetInstance].uid = [[dic objectForKey:@"uid"] intValue];
    }
    else {
        //result = [[JsonResult alloc] initWithJSONStr:myResponseStr];
    }
    NSLog(@"返回结果:%@",myResponseStr);
  

    //NSLog(@"jsonResult=%@",[result toJson]);
	//[self getTableView].pg = result.pg;
	
//	BOOL con = NO;
//	//提示用户需要登录
//	if ([result.suggestAction isEqualToString:@"openLoginDlg"]) {
//		result.isneedLogin = YES;
//		con = YES;
		MyTouchController *touch = [[MyTouchController alloc] init];
		touch.breakDelegate = self;
		[touch setBreakPage:BreakNeedLogin];
		[self PushToTop:touch];
		[touch release];
//	}
//	//弹出信息
//	else if ([result.suggestAction isEqualToString:@"alert"]){
//		[Global messagebox:result.msg];
//		con = YES;
//	}
//	else if([result.suggestAction isEqualToString:@"toast"]){
//		[Global messagetoast:result.msg target:self.view parent:self];
//		con = YES;
//	}
//	//服务端返回异常
//	if (con == NO && !result.success) {
//		//[Global messagebox:@"服务端异常!"];
//		[Global messagetoast:@"服务端异常" target:self.view parent:self];
//		NSError *error = [request error];
//		NSLog(@"请求时报错情况requestFailed:---%@",error);
//		NSLog(@"服务端返回的数据:%@",myResponseStr);
//	}
//	[myResponseStr release];
//    //NSLog(@"memory 请求完成=%f mb",[[UIDevice currentDevice] availableMemory]);
//    if (con) {
//        [self responseReturnSuccess:result];
//        return;
//    }
    //[self responseDataSuccess:result];
    [self responseReturnSuccess:myResponseStr];
  [myResponseStr release];
	//NSLog(@"memory 加载数据结束=%f mb",[[UIDevice currentDevice] availableMemory]);
}

-(void) responseReturnSuccess:(NSString*)json{
	if ([self getTableView].currentPage == 0) {
		[[self getTableView].tableArray removeAllObjects];
	}
}

// 请求失败
- (void)requestFailed:(ASIHTTPRequest *)request
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	NSError *error = [request error];
	[self _requestFailed:request.processType];
	NSLog(@"请求时报错情况requestFailed:---%@",error);
//	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"已经取消了网络请求!" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
//	[alert show];
//	[alert release];
}

-(void) _requestFailed:(int)processType{

	if (processType == ProcessDefault) {
		if (bvProgress!=nil) {
			[progress stopAnimating];
			[progress removeFromSuperview];
			[progress release];
			progress = nil;
			[bvProgress removeFromSuperview];
			[bvProgress release];
			bvProgress = nil;
		}
		
	}
	if (processType == ProcessSimple) {
		if (progress != nil) {
			[progress stopAnimating];
			[progress removeFromSuperview];
			[progress release];
			progress = nil;
		}
	}
	NSLog(@"请求时报错情况_requestFailed:---%@",@"这个网络请求连接失败");
//	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败, 请稍后重试!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
//	[alert show];
//	[alert release];
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值