IOS afnetworking 2.x JSON method

Source: Internet
Author: User

Written in front (nonsense, can be skipped) is a long time no update, mainly busy (lazy). Try to learn more and share more!

Formally started in the process of development, we will inevitably encounter network operations, we can use the original iOS network framework, of course, the use of the three-way framework is easier, very famous is the afnetworking framework, this framework seems to be the best tripartite framework in 12. In short, the iOS development, the network part, basically will use this framework.
This frame on GitHub, afnetworking portal--Dot I dot I
If you use the 1.x version of this framework, one of the APIs used in JSON parsing, but after 2.x, Afnetworking recommends using another API, and most of the tutorials on the Web are 1.x JSON APIs, which mainly share the next 2.x approach.

Let us first feel the 1.x.

Build URLs
NSString *urlstring = [NSString stringwithformat:@ "xxxxxxxxxx"];//xxxx write a URL for you//if there is Chinese in the URL, you need to convert urlstring = [Urlstri     Ng Stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];    Build Nsurl Nsurl *url = [Nsurl urlwithstring:urlstring]; Build request, this build method is a encapsulation of the basic building method is strengthened. The main time-out attribute is the last parameter, 4.0f. This means that if there is no response within 4 seconds, do not block the main thread (why do not repeat the main thread) nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: NSURLREQUESTUSEPROTOCOLCACHEPOLICYTIMEOUTINTERVAL:4.0F];
   //AF starts with the Afnetworking framework API, which is the 1.x approach.     Afjsonrequestoperation *op = [Afjsonrequestoperation jsonrequestoperationwithrequest:requestsuccess:^ ( Nsurlrequest *request, nshttpurlresponse *response, id JSON) {        nsdictionary * Dictionary = json;        int ret = [[Dictionary objectforkey:@ ' ret '] intvalue];  & nbsp;           //judgment on server return data         switch ( RET) {            case-1:            & nbsp   [self showalertwithstring:@ "user not logged in"];                break;             case 1:              & nbsp [Self savedatatocurrentaccount];                [self SHOWALERTWITHDelegatewithstring:@ "saved successfully"];                break;             case-2:                [self sh owalertwithstring:@ "Save Failed"];                break;         }   } failure:^ (Nsurlrequest *request, Nshttpurlresponse *response, Nserror *error, ID JSON) {        [self showalertwithstring:[nsstring stringwithformat:@ "%@", error.localizeddescription]];   }];    [op start];
I believe that we can read,
It's good to use block if successful, Todo, if failed, Tudo. (Success, failure inside the method is my own encapsulation, you write your own OK)
And the whole method is put into operation.
Direct the returned JSON to you in the form of a parameter.

This is the 1.x JSON method, but if the use of a 2.x framework, then use this method will be error, because the 2.x framework canceled afjsonrequestoperation
What should we do at this time?


Let everyone feel the next 2.x
NSString *urlstring = [NSString stringwithformat:@ "xxxxxxxx"];    There is Chinese, need to convert urlstring = [urlstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];    Nsurl *url = [Nsurl urlwithstring:urlstring]; Nsurlrequest *request = [Nsurlrequest requestwithurl:url cachepolicy: NSURLREQUESTUSEPROTOCOLCACHEPOLICYTIMEOUTINTERVAL:4.0F];
Afhttprequestoperation *op = [[Afhttprequestoperation alloc] initwithrequest:request];    Op.responseserializer = [Afjsonresponseserializer serializer]; [Op setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject) {
Nsdictionary *dictionary = Responseobject; int ret = [[Dictionary objectforkey:@ "ret"] intvalue];
        //judgment on server return data         switch (ret) {             case-1:                [self sh owalertwithstring:@ "User not logged in"];                break;             case 1:                [self sav edatatocurrentaccount];                [self showalertwithdelegatewithstring:@ "saved successfully"];                break;             case-2:              & nbsp [Self showalertwithstring:@ "save failed"];                break;        }   } failure:^ (afhttprequestoperation *operation, Nserror *error) {        [self showalertwithstring:[nsstring stringwithformat:@ "%@", Error.localizeddescription]];
}]; [[Nsoperationqueue Mainqueue] addoperation:op];

In 2.x, he used afhttprequestoperation and Afjsonresponseserializer to serialize the results with his own API, when there was no ID JSON, but you took responseobject as the ID JSON is OK with it.


Finally, I think we should all know, but write it out.
How do I import frames?
Give me two ways.
One. Go directly to the portal to find the necessary download, generally we use the first, also is the default afnetworking framework. Go in, look at the right side, there is a download, download, decompression, there is a folder, look for, is the framework of. H.M, specific file structure forget, but very easy to find. Then drag this folder into the project and choose Copy,group, your target. Then import the AFNetworking.h where it is needed to be OK.
Two. Using Cocoapods Configuration

IOS afnetworking 2.x JSON method

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.