Web request for iOS development (Afnetworking-based re-encapsulation)

Source: Internet
Author: User

I've been busy lately and haven't had time to write blogs. Take a break for a holiday, write a blog to summarize the recent problems that have occurred in the project!!!

First of all, in the project I played a role, nothing more than the art (UI designer) to show us the diagram, and then interact with the server.

How do we interact with the server? The simplest way is to speak, but we have to talk to the server through the words we can listen to each other to communicate, also need to make a lot of agreements (HTTP,TCP,UDP, this interested friend can learn about the Internet Protocol). But what I usually use most often is the HTTP protocol.

http://www.cnblogs.com/li0803/archive/2008/11/03/1324746.html this blog details the HTTP protocol of things (I can say that I casually Baidu a blog?) )

Then the afnetworking on GitHub should now be the most widely used Web request in the three-party library. So today, let's say afnetworing.

Why do we have to superfluous the package now that they have been packaged so well?

Now, let's just say, what is the meaning of the re-encapsulation?

The most important meaning is almost unified interface (this is also a role of encapsulation, if you do not know the packaging of the students can go to see the use of encapsulation, is a very effective way to improve efficiency), this is a lot of people in the project is particularly reflected in, one of my colleagues with the system comes with the mknetworking , a lot of people may not have heard of, although he used very well, I can read, but still feel very uncomfortable, because I do not want to see his specific usage and implementation of what it looks like (although the real knock up may be just two lines of code only), only need to tune an interface on everything is good, Why do I have to knock on so many repetitive codes (Don ' t repeat yourself)?

Okay, here's what I'm going to do. afnetworking Re-encapsulation:

First of all, or to say the package of things, if you use yourself, then you can write it casually, because you can read without comments. But if it is for a few people, you can tell them directly, or write a more detailed comment, so that they forget to look at the future to know what the meaning of each parameter is!!!

So what are the most commonly used network requests when I'm on a network request?

The most common thing I used to do was get,post and upload. (These requests can also be used to understand their differences)

Say so much nonsense to have some dry goods.

@interfaceUploadparam:nsobject/** * Binary data for images*/@property (nonatomic, strong) NSData*data;/** * server-corresponding parameter name*/@property (nonatomic, copy) NSString*name;/** * The name of the file (the name of the server saved after uploading to the server)*/@property (nonatomic, copy) NSString*filename;/** * File MIME type (image/png,image/jpg, etc.)*/@property (nonatomic, copy) NSString*MimeType;@end

@classUploadparam;/** * Network request type*/typedef ns_enum (nsuinteger,httprequesttype) {/** * GET request*/Httprequesttypeget=0,    /** * POST request*/Httprequesttypepost};@interfaceHttprequest:nsobject/** * Send GET request * * @param urlstring request URL String * @param parameters Request parameter * @param success Request Successful callback * @param failure Callback for failed requests*/+ (void) Getwithurlstring: (NSString *) urlstring parameters: (ID) Parameters success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) Failure;/** * Send POST request * * @param URL string for urlstring request * @param parameter of Parameters request * @param success Request Successful callback * @param Failur E callback for failed requests*/+ (void) Postwithurlstring: (NSString *) urlstring parameters: (ID) Parameters success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) Failure;/** * send a network request * * @param the URL string of the urlstring request * @param the parameter of the parameters request * @param type of request * @param RESULTB The result of the lock request*/+ (void) Requestwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Type: (httprequesttype) Type success: (void(^) (IDresponseobject)) Success Failure: (void(^) (Nserror *error)) Failure;/** * Upload image * * @param urlstring upload image URL String * @param parameters upload Image parameters * @param uploadparam upload image information * @param suc Cess Upload Successful callback * @param failure upload failed callback*/+ (void) Uploadwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Uploadparam: (Uploadparam*) Uploadparam success: (void(^) ()) Success failure: (void(^) (Nserror *error)) Failure
#import "HttpRequest.h"#import "AFNetworking.h"#import "UploadParam.h"@implementationHttpRequest#pragmaMark--GET Request--+ (void) Getwithurlstring: (NSString *) urlstring parameters: (ID) Parameters success: (void(^) (ID)) Success failure: (void(^) (Nserror *)) Failure {Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; /** * Acceptable type*/Manager.responseserializer=[Afhttpresponseserializer Serializer]; /** * Maximum number of concurrent requests queue*///manager.operationQueue.maxConcurrentOperationCount = 5;    /** * Request time-out*///manager.requestSerializer.timeoutInterval = 5;[Manager get:urlstring Parameters:nil Progress:nil success:^ (nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {        if(Success) {success (responseobject); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {        if(failure) {failure (error); }    }];}#pragmaMark--POST request--+ (void) Postwithurlstring: (NSString *) urlstring parameters: (ID) Parameters success: (void(^) (ID)) Success failure: (void(^) (Nserror *)) Failure {Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:urlstring Parameters:parameters Progress:nil success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {        if(Success) {success (responseobject); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {        if(failure) {failure (error); }    }];}#pragmaMark--Post/get network Request--+ (void) Requestwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Type: (httprequesttype) Type success: (void(^) (ID)) Success failure: (void(^) (Nserror *)) Failure {Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; Switch(type) { CaseHttprequesttypeget: {[manager get:urlstring Parameters:nil Progress:nil success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {                if(Success) {success (responseobject); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {                if(failure) {failure (error);        }            }]; }             Break;  CaseHttprequesttypepost: {[manager post:urlstring parameters:parameters Progress:nil success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {                if(Success) {success (responseobject); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {                if(failure) {failure (error);        }            }]; }             Break; }}#pragmaMark--Upload image--+ (void) Uploadwithurlstring: (NSString *) urlstring parameters: (ID) Parameters Uploadparam: (Uploadparam*) Uploadparam success: (void(^) ()) Success failure: (void(^) (Nserror *)) Failure {Afhttpsessionmanager*manager =[Afhttpsessionmanager Manager]; Manager.responseserializer=[Afhttpresponseserializer Serializer]; [Manager post:urlstring parameters:parameters Constructingbodywithblock:^(ID<AFMultipartFormData>_nonnull FormData) {[FormData appendPartWithFileData:uploadParam.data name:uploadParam.name fileName:uploadParam.filename mimetype:up    Loadparam.mimetype]; } Progress:nil Success:^ (Nsurlsessiondatatask * _nonnull task,ID_nullable Responseobject) {        if(Success) {success (responseobject); }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror *_nonnull Error) {        if(failure) {failure (error); }    }];}

I sent my demo to my space, welcome to download. I use the cocospod three-party library, if the download can not be used, you can also contact me!

Web request for iOS development (Afnetworking-based re-encapsulation)

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.