2014年09月11日

来源:互联网 发布:vscode开发微信小程序 编辑:程序博客网 时间:2024/06/08 07:01
如何选择AFNetworking版本

首先得下载AFNetworking库文件,下载时得首先弄清楚,你将要开发的软件兼容的最低版本是多少。AFNetworking 2.0或者之后的版本需要xcode5.0版本并且只能为IOS6或更高的手机系统上运行,如果开发MAC程序,那么2.0版本只能在MAC OS X 10.8或者更高的版本上运行。

AFNetworking 2.0的下载地址https://github.com/AFNetworking/AFNetworking
如果你想要兼容IOS5或MAC OS X 10.7,那你需要用最新发布的1.x版本
AFNetworking 1.x的下载地址https://github.com/AFNetworking/AFNetworking/tree/1.x
如果要兼容4.3或者MAC OS X 10.6,需要用最新发布的0.10.x版本
AFNetworking 0.10.xhttps://github.com/AFNetworking/AFNetworking/tree/0.10.x

2013年大多数软件兼容的最低版本为4.3,而2014年,估计大多数软件兼容的最低版本将会是5.0甚至6.0;
所以,目前最好的选择还是1.x版本,兼容到IOS5.0
如何通过URL获取json数据
第一种,利用AFJSONRequestOperation,官方网站上给的例子:

    NSString *str=[NSString stringWithFormat:@"https://alpha-api.app.net/stream/0/posts/stream/global"];

    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //    URL获取json数据

    AFJSONRequestOperation *operation1 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary* JSON) {

                NSLog(@"获取到的数据为:%@",JSON);

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id data) {

        NSLog(@"发生错误!%@",error);

    }];

    [operation1 start];


第二种方法,利用AFHTTPRequestOperation 先获取到字符串形式的数据,然后转换成json格式,将NSString格式的数据转换成json数据,利用IOS5自带的json解析方法:

   NSString *str=[NSString stringWithFormat:@"https://alpha-api.app.net/stream/0/posts/stream/global"];

    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

   AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, idresponseObject) {

        NSString *html = operation.responseString;

             NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];

             id dict=[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];

        NSLog(@"获取到的数据为:%@",dict);

    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"发生错误!%@",error);

    }];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [queue addOperation:operation];



如果发生Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x14defc80 {NSUnderlyingError=0x14deea10 "bad URL", NSLocalizedDescription=bad URL这个错误,请检查URL编码格式。有没有进行stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding



如何通过URL获取图片

异步获取图片,通过队列实现,而且图片会有缓存,在下次请求相同的链接时,系统会自动调用缓存,而不从网上请求数据。


UIImageView *imageView = [[UIImageView allocinitWithFrame:CGRectMake(0.0f, 100.0f, 100.0f, 100.0f)]; 
    [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"]placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]]; 
    [self.view addSubview:imageView];

上面的方法是官方提供的,还有一种方法,

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.scott-sherwood.com/wp-content/uploads/2013/01/scene.png"]];

    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse*response, UIImage *image) {

        self.backgroundImageView.image = image;

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

        NSLog(@"Error %@",error);

    }];

 

    [operation start];


如果使用第一种URLWithString:  placeholderImage:会有更多的细节处理,其实实现还是通过AFImageRequestOperation处理,可以点击URLWithString:  placeholderImage:方法进去看一下就一目了然了。所以我觉得还是用第一种好。


如何通过URL获取plist文件

通过url获取plist文件的内容,用的很少,这个方法在官方提供的方法里面没有

    NSString *weatherUrl = @"http://www.calinks.com.cn/buick/kls/Buickhousekeeper.plist";

    NSURL *url = [NSURL URLWithString:[weatherUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [AFPropertyListRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];

    AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:^(NSURLRequest *request,NSHTTPURLResponse *response, id propertyList) {

        NSLog(@"%@",(NSDictionary *)propertyList);

        

    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, idpropertyList) {

        NSLog(@"%@",error);

    }];

 

    [operation start];

如果稍不留神,可能就出现Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(

    "application/x-plist"

)}, got text/plain" UserInfo=0x16e91ce0 {NSLocalizedRecoverySuggestion=

 

...

...

, AFNetworkingOperationFailingURLRequestErrorKey= { }, NSErrorFailingURLKey=, NSLocalizedDescription=Expected content type {(

    "application/x-plist"

)}, got text/plain, AFNetworkingOperationFailinponseErrorKey= { URL:  } { status code: 200, headers {

    "Accept-Ranges" = bytes;

    Connection = "keep-alive";

    "Content-Length" = 974;

    "Content-Type" = "text/plain";

    Date = "Sat, 25 Jan 2014 07:29:26 GMT";

    Etag = ""1014c2-3ce-4ee63e1c80e00"";

    "Last-Modified" = "Wed, 25 Dec 2013 23:04:24 GMT";

    Server = "nginx/1.4.2";

 

} }}

可能还会出现乱码,解决办法就是[AFPropertyListRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];

0 0