An asynchronous way to encapsulate a get and post

Source: Internet
Author: User

H wrote it inside.

#import <Foundation/Foundation.h>

typedef void (^myblock) (ID object);

@interface Networkhandle:nsobject



+ (void) getdatawithurlstring: (NSString *) string compare: (Myblock) block;


+ (void) postdatawithurlstring: (NSString *) string
Andbodystring: (NSString *) bodystring
Compare: (Myblock) block;
@end

It's written inside M.

#import "NetworkHandle.h"
@interface Networkhandle ()
@property (nonatomic, strong) Nsmutabledata *data;
@end

@implementation Networkhandle

+ (void) getdatawithurlstring: (NSString *) string compare: (Myblock) block{

Do a UTF-8 to the address to prevent the parameter inside the Chinese
NSString *urlstr = [string stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Convert Address to URL format
Nsurl *url = [Nsurl urlwithstring:urlstr];
Set Request mode
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
[Request sethttpmethod:@ "GET"];
Synchronous or asynchronous
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
if (data! = nil) {
Because the type of the data is not determined, the ID generic pointer is used to receive
ID object = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil];
Block (object);
}

}];

}


+ (void) postdatawithurlstring: (NSString *) string andbodystring: (NSString *) bodystring compare: (Myblock) block{

NSString *urlstr = [string stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
Nsurl *url = [Nsurl urlwithstring:urlstr];
Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];
NSString *parstr = bodystring;
NSData *patdata = [Parstr datausingencoding:nsutf8stringencoding];

[Request sethttpmethod:@ "POST"];
[Request Sethttpbody:patdata];
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {
if (data! = nil) {
ID object = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil];
Block (object);
}

}];

}

An asynchronous way to encapsulate a get and post

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.