iOS development--use afnetworking 3.1.0, simple Request encapsulation class

Source: Internet
Author: User

From the Apple system to bring the request class, to asihttprequest third-party request class, and then to afnetworking third-party request class, currently as long as the data request, is basically used AFN, so, here is purely recorded:

HRequestTools.h

#import<Foundation/Foundation.h>/** * Network request type*/typedef ns_enum (nsuinteger,httprequesttype) {/** * GET request*/Httprequesttypeget=1,    /** * POST request*/Httprequesttypepost=2};@interfaceHrequesttools:nsobject/** * send a network request * * @param the URL string of the urlstring request * @param the parameter of the parameters request * @param type of request * @param succes The result of the S request*/+ (void) Requestwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Type: (httprequesttype) Type success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) Failure

Hrequesttools.m

#pragmaThe request method of the Mark Package + (void) Requestwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Type: (httprequesttype) Type success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) failure{Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; Manager.requestSerializer.timeoutInterval=30.0f; Switch(type) { CaseHttprequesttypeget: {[manager get:urlstring parameters:parameters success:^ (Nsurlsessiondatatask *task,IDresponseobject) {                if(Success) {IDJsons =[nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers Error:nil];                Success (jsons); }} failure:^ (Nsurlsessiondatatask *task, Nserror *error) {                if(failure) {failure (error);        }            }]; }             Break;  Casehttprequesttypepost: {if(Internets) {//when there is a networkAfhttpsessionmanager *manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:urlstring parameters:parameters success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {                    if(Success) {IDJsons =[nsjsonserialization jsonobjectwithdata:responseobject options:nsjsonreadingmutablecontainers Error:nil];                    Success (jsons); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {                    if(failure) {failure (error); [Htools Showtextonlyhud:@"The data request failed, please try again. "Delay2.0];            }                }]; }Else //when there is no network{[Htools Showtextonlyhud:@"Please check your network"Delay2.0]; Nserror*error =Nil;            Failure (error); }                    }             Break; default:             Break; }        }

If you want to add a cache in the request, you can use this third party with Yycache,

About Yycache, you can recommend a few links: Http://www.jianshu.com/p/b8dcf6634fab

iOS development--use afnetworking 3.1.0, simple Request encapsulation class

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.