解决方案 »

  1.   

    你已经设置了delegate,可以看看NSURLConnectionDataDelegate这个类,具体的,实现connection:didReceiveResponse:之类的就行了。
      

  2.   

    使用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];
      

  3.   


    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    主要的操作是在这个方法里面完成的吗?
      

  4.   

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data我需要在上述方法中哪一个接收到服务器端返回的数据,然后根据其返回的数据进行判断?
      

  5.   

    - (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没有问题,我点了注册后,却没有反应,麻烦版主给看看。
      

  6.   

    如果我没有看错的话,你的请求都没有发出去,缺少:
    [connection start];
      

  7.   

    我最近也在研究这个问题,楼主做好后,demo能不能发我一个,QQ1828141617
      

  8.   

    想清楚了,发送获取验证码时不用实现delegate,把请求抛出去就OK了。
    搞定了。。