Open source China iOS client Learning (eight) network communication afnetworking Class library

Source: Internet
Author: User
Tags config

Afnetworking is a lightweight iOS network communications library, after the ASI class library is not updated after the developers have a good choice;

Afnetworking class Library Source code download and use tutorial: https://github.com/AFNetworking/AFNetworking

If you want to delve into the official documentation: http://afnetworking.github.com/AFNetworking/

The use of the Afnetworking class library in the open source China iOS client uses only two instance methods

(1) GetPath:parameters:success:failure:

(2) PostPath:parameters:success:failure:

Their usage is essentially the same, except that the request data is in a different way, a GET request and a POST request. Get is a request to the server to ask for data, which is equivalent to the query information function, will not modify the class capacity, post is a request to the server to submit data, affect the content of the data, two methods of definition:

-(void) GetPath: (NSString *) path parameters: (nsdictionary *) Parameters success: (void (^) (afhttpre Questoperation *operation, ID responseobject) success failure: (void (^) (afhttprequestoperation *operation, NSERR  
    or *error) Failure {nsurlrequest *request = [self requestwithmethod:@ ' get ' path:path parameters:parameters];  
    Afhttprequestoperation *operation = [self httprequestoperationwithrequest:request success:success failure:failure];  
[Self enqueuehttprequestoperation:operation]; }
-(void) Postpath: (NSString *) path   
      parameters: (nsdictionary *) Parameters   
         success: (void (^) ( Afhttprequestoperation *operation, id responseobject) Success  
         failure: (void (^) (afhttprequestoperation * Operation, Nserror *error)) failure  
{  
    nsurlrequest *request = [self requestwithmethod:@ ' POST ' Path:path Parameters:parameters];  
    Afhttprequestoperation *operation = [self httprequestoperationwithrequest:request success:success failure:failure];  
    [Self enqueuehttprequestoperation:operation];  
}

GetPath:parameters:success:failure: Methods use examples in programs:

newsview.m

-(void) Reload: (BOOL) Norefresh {//If there is a network connection if ([Config instance].isnetworkrunning) {if (Isloadin G | |  
        Isloadover) {return;  
        } if (!norefresh) {allcount = 0;  
        int pageIndex = ALLCOUNT/20;  
      
        NSString *url; Switch (self.catalog) {Case 1:url = [NSString stringwithformat:@%@?catalog=%d&pagei  
                ndex=%d&pagesize=%d ", Api_news_list, 1, PageIndex, 20];  
            Break Case 2:url = [NSString stringwithformat:@ "%@?type=latest&pageindex=%d&pagesize=%d", Api_blog_li  
                St, PageIndex, 20];  
            Break Case 3:url = [NSString stringwithformat:@ "%@?type=recommend&pageindex=%d&pagesize=%d", Api_blog  
                _list, PageIndex, 20];  
        Break   
                  } [[Afoscclient Sharedclient]getpath:url Parameters:nil
          success:^ (afhttprequestoperation *operation, id responseobject) {[Tool get  
            OSCNotice2:operation.responseString];  
            isloading = NO;  
            if (!norefresh) {[Self clear];  
                      
                } @try {Nsmutablearray *newnews = Self.catalog <= 1? [Tool readStrNewsArray:operation.responseString andold:news]: [Tool Readstruserblogsar  
                Ray:operation.responseString Andold:news];  
                int count = [Tool isListOver2:operation.responseString];  
                Allcount + = count;  
                if (Count <) {Isloadover = YES;  
                [News addobjectsfromarray:newnews];  
                [Self.tablenews Reloaddata];  
                      
                [Self doneloadingtableviewdata];  
      If it's the first page, it's cached.          if (News.count <=) {[Tool savecache:5 andID:self.catalog AndString:operation.respon  
                Sestring]; }} @catch (NSException *exception) {[Nduncaughtexceptionhandler takeexcept  
            Ion:exception];  
            } @finally {[self doneloadingtableviewdata];  
            } failure:^ (Afhttprequestoperation *operation, Nserror *error) {NSLog (@ "News list gets an error");  
                  
            If you are refreshing [self doneloadingtableviewdata];  
            if ([Config instance].isnetworkrunning = = NO) {return;  
            } isloading = NO; if ([Config instance].isnetworkrunning) {[Tool toastnotification:@ error Network connectionless] AndView:self.view Andloadi  
            Ng:no Andisbottom:no];  
        }  
        }];  
        isloading = YES;  
    [Self.tablenews Reloaddata]; }//ASFruit No network connection else {nsstring *value = [Tool getcache:5 andID:self.catalog];  
            if (value) {Nsmutablearray *newnews = [Tool readstrnewsarray:value andold:news];  
            [Self.tablenews Reloaddata];  
            Isloadover = YES;  
            [News addobjectsfromarray:newnews];  
            [Self.tablenews Reloaddata];  
        [Self doneloadingtableviewdata]; }  
    }  
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.