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 is on GitHub, afnetworking portals-- Dot Me dot i if I use the 1.x version of this framework, one of the APIs used in JSON parsing, but after 2.x, Afnetworking recommends the use of another API, and most of the online tutorials are 1.x JSON API, this article mainly share the next 2.x method.
Let us first feel the 1.x.Build URLs

NSString *urlstring = [nsstringstringwithformat:@ "xxxxxxxxxx"];// xxxx to write your website


//If you have Chinese in the URL, you need to convert

urlstring = [urlstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];


Build Nsurl

nsurl *url = [nsurlurlwithstring: 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, the main thread will not be blocked (why not repeat the main thread)

nsurlrequest *request = [nsurlrequestrequestwithurl: URL CachePolicy:nsurlrequestuseprotocolcachepolicytimeoutinterval:4.0f] ;

Starting with AF is the Afnetworking Framework API, which is the 1.x approach.

afjsonrequestoperation *op = [afjsonrequestoperationjsonrequestoperationwithrequest: Request Success: ^ (nsurlrequest *request, nshttpurlresponse *response, ID JSON) {

nsdictionary *dictionary = JSON;

int ret = [[Dictionary Objectforkey:@ "ret"] intvalue];

// judgment on server return data

Switch (ret) {

Case-1:

[selfshowalertwithstring:@ " user not logged in "];

break;

Case 1:

[self savedatatocurrentaccount];

[self showalertwithdelegatewithstring:@ " save successfully " ];

break;

Case-2:

[selfshowalertwithstring:@ " save failed "];

break;

}

}Failure: ^ (nsurlrequest *request,nshttpurlresponse *response, nserror *error, ID JSON) {

[selfshowalertwithstring: [nsstring stringWithFormat:@ "%@", error. Localizeddescription]];

}];

[opstart];


I believe that we can understand, it is very good use of 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 into the 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, and then use this method will be an error,because the 2.x frame was canceled.afjsonrequestoperationWhat should we do at this time?
let everyone feel the next 2.x

NSString *urlstring = [nsstringstringwithformat:@ "xxxxxxxx"];

// have Chinese, need to convert

urlstring = [urlstring stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];

nsurl *url = [nsurlurlwithstring: urlstring];

nsurlrequest *request = [nsurlrequestrequestwithurl: URL CachePolicy:nsurlrequestuseprotocolcachepolicytimeoutinterval:4.0f ];

afhttprequestoperation *op = [[afhttprequestoperationalloc] Initwithrequest: request];

op. Responseserializer = [afjsonresponseserializerserializer];

[Opsetcompletionblockwithsuccess: ^ (afhttprequestoperation *operation,ID Responseobject) {

nsdictionary *dictionary = responseobject;

int ret = [[Dictionary Objectforkey:@ "ret"] intvalue];


// judgment on server return data

Switch (ret) {

Case-1:

[selfshowalertwithstring:@ " user not logged in "];

break;

Case 1:

[self savedatatocurrentaccount];

[self showalertwithdelegatewithstring:@ " save successfully " ];

break;

Case-2:

[selfshowalertwithstring:@ " save failed "];

break;

}

}Failure: ^ (afhttprequestoperation *operation,nserror *error) {

[selfshowalertwithstring: [nsstring stringWithFormat:@ "%@", error. localizeddescription ]];

}];

[[nsoperationqueuemainqueue] addoperation: op];


In 2.x, he used theafhttprequestoperationand Afjsonresponseserializer, the result is serialized with his own API, this time, there is no ID json, but you put Responseobject as ID json with OK.
Finally, I think we should all know, but write it out. How do I import frames? Give me two ways to do it. 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.