从服务器接收返回信息 IOS服务器delegateurl 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你已经设置了delegate,可以看看NSURLConnectionDataDelegate这个类,具体的,实现connection:didReceiveResponse:之类的就行了。 使用NSURLConnection获取服务器端的返回,方法有两种1:使用delegate ,在NSURLConnection的代理协议中有处理数据返回的方法2. 使用block 方式 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) { id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];} -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data主要的操作是在这个方法里面完成的吗? -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data我需要在上述方法中哪一个接收到服务器端返回的数据,然后根据其返回的数据进行判断? - (IBAction)Submitclick:(id)sender{ [self saveRegAccount];}//服务器请求-(void) saveRegAccount{ NSURL *url = [NSURL URLWithString:@"http://192.168.1.100:8080/world/mini/api/httpservice/kiwi/world/worldAccountService/saveRegAccount.xhtml"]; NSString *post=[NSString stringWithFormat:@"number=%@&pwd=%@&code=%@",self.txtphoneNum.text,self.txtpassworld.text,self.txtVerificationNum.text]; NSLog(@"============post:%@",post); NSData *Logininformation=[post dataUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:Logininformation]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; //设置请求超时 NSMutableURLRequest *requesttime=[NSMutableURLRequest requestWithURL:url]; requesttime.timeoutInterval=5.0; }//当服务器的数据加载完毕时就会调用-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"服务器的数据加载完毕"); //隐藏HUD [tooles removeHUD]; //处理服务器返回的所有数据 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:nil]; //判断 NSString *error=dict[@"-1"]; NSString *error1=dict[@"1"]; NSString *error2=dict[@"0"]; if(error) { [Utils alertTitle:@"提示" message:@"动态口令错误" delegate:self cancelBtn:@"OK" otherBtnName:nil]; }else if(error1) { [Utils alertTitle:@"提示" message:@"注册成功" delegate:self cancelBtn:@"OK" otherBtnName:nil]; }else if(error2) { [Utils alertTitle:@"提示" message:@"注册失败,请重试..." delegate:self cancelBtn:@"OK" otherBtnName:nil]; }else { NSLog(@"==================未知错误"); } }#pragma - NSURLConnectionDataDelegate代理方法//当接收到服务器的响应(连通了服务器)时会调用-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"=====联通了服务器"); self.responseData=[NSMutableData data]; } //当接收到服务器的数据时会调用 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"接收到服务器的数据"); //拼接数据 [self.responseData appendData:data]; NSLog(@"%lu---%@--",(unsigned long)self.responseData.length,[NSThread currentThread]);} //请求错误(失败)的时候调用(请求超时\断网\没有网\,一般指客户端错误) -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ [tooles removeHUD]; [Utils alertTitle:@"提示" message:@"网络繁忙,请重试..." delegate:self cancelBtn:@"OK" otherBtnName:nil];}这是我的最终代码,run没有问题,我点了注册后,却没有反应,麻烦版主给看看。 如果我没有看错的话,你的请求都没有发出去,缺少:[connection start]; 我最近也在研究这个问题,楼主做好后,demo能不能发我一个,QQ1828141617 想清楚了,发送获取验证码时不用实现delegate,把请求抛出去就OK了。搞定了。。 ios自带地图首次定位问题 创建UITableViewCell apple developer账号添加测试设备限制问题 ios 摄像头 没有类似安卓上startprivew和stopprivew的功能 FMDatabase库中数据库的查询 我用的NSUserDeafaul存东西报错~~~ 说我类型不对~ 求大神~ 求助,分真心不多了,关于http异步请求接收到的数据 如何用这些数据来初始化view呢? 子视图之间保持相对位置不变的方法 多线程问题 iOS charts 拐点气泡文字设置 数组指针?object-c翻译成swift,求指导~ ios设备上面显示电池和信号的那条矩形叫什么
1:使用delegate ,在NSURLConnection的代理协议中有处理数据返回的方法
2. 使用block 方式
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) {
id res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
主要的操作是在这个方法里面完成的吗?
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data我需要在上述方法中哪一个接收到服务器端返回的数据,然后根据其返回的数据进行判断?
{
[self saveRegAccount];
}
//服务器请求
-(void) saveRegAccount{
NSURL *url = [NSURL URLWithString:@"http://192.168.1.100:8080/world/mini/api/httpservice/kiwi/world/worldAccountService/saveRegAccount.xhtml"];
NSString *post=[NSString stringWithFormat:@"number=%@&pwd=%@&code=%@",self.txtphoneNum.text,self.txtpassworld.text,self.txtVerificationNum.text];
NSLog(@"============post:%@",post);
NSData *Logininformation=[post dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:Logininformation];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
//设置请求超时
NSMutableURLRequest *requesttime=[NSMutableURLRequest requestWithURL:url];
requesttime.timeoutInterval=5.0;
}
//当服务器的数据加载完毕时就会调用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"服务器的数据加载完毕");
//隐藏HUD
[tooles removeHUD];
//处理服务器返回的所有数据
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:nil];
//判断
NSString *error=dict[@"-1"];
NSString *error1=dict[@"1"];
NSString *error2=dict[@"0"];
if(error)
{
[Utils alertTitle:@"提示" message:@"动态口令错误" delegate:self cancelBtn:@"OK" otherBtnName:nil];
}else if(error1)
{
[Utils alertTitle:@"提示" message:@"注册成功" delegate:self cancelBtn:@"OK" otherBtnName:nil];
}else if(error2)
{
[Utils alertTitle:@"提示" message:@"注册失败,请重试..." delegate:self cancelBtn:@"OK" otherBtnName:nil];
}else
{
NSLog(@"==================未知错误");
}
}
#pragma - NSURLConnectionDataDelegate代理方法//当接收到服务器的响应(连通了服务器)时会调用
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"=====联通了服务器");
self.responseData=[NSMutableData data];
} //当接收到服务器的数据时会调用
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"接收到服务器的数据");
//拼接数据
[self.responseData appendData:data];
NSLog(@"%lu---%@--",(unsigned long)self.responseData.length,[NSThread currentThread]);
} //请求错误(失败)的时候调用(请求超时\断网\没有网\,一般指客户端错误) -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[tooles removeHUD];
[Utils alertTitle:@"提示" message:@"网络繁忙,请重试..." delegate:self cancelBtn:@"OK" otherBtnName:nil];
}
这是我的最终代码,run没有问题,我点了注册后,却没有反应,麻烦版主给看看。
[connection start];
搞定了。。