ASIHTTPRequest发送异步GET请求
*/
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
// 获取URL
NSURL *url = [NSURLURLWithString:@"http://192.168.15.56:8080/MJServer/login?username=123&pwd=123"];
// 创建一个请求对象
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
request.timeOutSeconds= 15;// 15秒后超时
// 设置代理
request.delegate= self;
// 开始请求
[request startAsynchronous];
}
#pragma mark -实现
/**
* 开始请求
*/
- (void)requestStarted:(ASIHTTPRequest*)request
{
NSLog(@"requestStarted");
}
/**
* 接收服务器返回的响应头信息
*/
- (void)request:(ASIHTTPRequest*)request didReceiveResponseHeaders:(NSDictionary*)responseHeaders
{
NSLog(@"%@",responseHeaders);
NSLog(@"didReceiveResponseHeaders");
}
/**
* 请求结束
*/
- (void)requestFinished:(ASIHTTPRequest*)request
{
NSLog(@"%@",request);
NSLog(@"requestFinished");
}
/**
* 请求失败
*/
- (void)requestFailed:(ASIHTTPRequest*)request
{
NSLog(@"requestFailed");
}
- (void)requestRedirected:(ASIHTTPRequest*)request
{
NSLog(@"requestRedirected");
}
/**
* 接收服务器返回的数据
*/
- (void)request:(ASIHTTPRequest*)request didReceiveData:(NSData*)data
{
// 解析数据
NSDictionary * dict = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableLeaveserror:nil];
NSString *error = dict[@"error"];
NSLog(@"%@",error);
NSString *success = dict[@"success"];
NSLog(@"%@",success);
NSLog(@"didReceiveData");
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。