解决方案 »
- 我开发蓝牙外置用的corebluetooth,怎么样确定我的UUID?
- 有没有做扫描功能的达人啊
- 新手求助:我注册的99美元公司型开发者账户,能在windows中VM7下安装的IOS系统里,让程序在真机上跑吗?
- ipod touch 越狱真机调试
- CCOrbitCamera 参数详解
- 新手求教 关于NSURL/NSConnection的问题
- 什么是第三方框架?
- 请问静态库的生成的文件路径在哪里修改?
- CoreText的CTLineGetGlyphRuns返回个数问题
- uiview中加载tableview ,cell的点击事件不触发
- 数组指针?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];
搞定了。。