ASIHttpRequst 之异步请求

来源:互联网 发布:微信无法打开淘宝链接 编辑:程序博客网 时间:2024/06/07 00:51

//

//  RootViewController.m

//  ASIHttpRequest

//

//  Created by Hallin_Me on 14-10-7.

//  Copyright (c) 2014 hallin_me. All rights reserved.

//

/*

  使用方法:

  ASIHttpRequst第三方类库 不支持arc 

  使用前导入下面4个系统包

   libz.1.1.3.dylib,MobileCoreServices.framework,SystemConfiguration.framework,CFNetwork.framework

*/

#define URL_PATH @"http://sp.autohome.com.cn/clubapp/jingxuan/HomeJingHua.ashx?pageSize=20&pageIndex=0"

#import "RootViewController.h"

#import "ASIHTTPRequest.h"

@interface RootViewController ()


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    //ASI请求

    [selfcreateASIHttp];

    self.view.backgroundColor=[UIColorredColor];

}

-(void)createASIHttp{

    //封装URL

   NSURL *url=[NSURLURLWithString:URL_PATH];

    //创建ASIHttp请求对象

    ASIHTTPRequest *request=[ASIHTTPRequestrequestWithURL:url];

    //协议

    request.delegate=self;

    //开始请求,开启异步请求

    [request startSynchronous];

}


#pragma mark ASIHTTPRequestDelegate

-(void)requestStarted:(ASIHTTPRequest *)request{

    NSLog(@"开始请求!");

}

-(void)request:(ASIHTTPRequest *)request didReceiveResponseHeaders:(NSDictionary *)responseHeaders{

    NSLog(@"建立联系!");

}

-(void)requestFinished:(ASIHTTPRequest *)request{

//    NSString *str=[[NSString alloc]initWithData:request.responseData encoding:NSUTF8StringEncoding];

//    NSLog(@"%@",str);

    //如若上面NSUTF8StringEncoding格式转换没用,链接的数据使用的GBK格式,所以data数据要格式转换

    NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

   NSString *str=[[NSStringalloc]initWithData:request.responseDataencoding:gbkEncoding];

   NSLog(@"%@",str);

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

0 0
原创粉丝点击