AFN synchronous asynchronous request and afn synchronous asynchronous request

Source: Internet
Author: User

AFN synchronous asynchronous request and afn synchronous asynchronous request

Asynchronous request:

-(BOOL) getOnlyKey1 {NSString * myUUIDStr = [[UIDevice currentDevice] identifierForVendor] UUIDString]; _ block bool isTrue = false; AFHTTPRequestOperationManager * manager = [Queue manager]; manager. responseSerializer. acceptableContentTypes = [NSSet setWithObject: @ "text/plain"]; NSString * urlstr = [NSString stringWithFormat: @ "http: // 122.225.89.70: 28080/try/check"]; NSURL * url = [NSURL URLWithString: urlstr]; NSDictionary * dic =@{@ "imei": myUUIDStr, @ "av": AppVersion}; [manager POST: urlstr parameters: dic success: ^ (AFHTTPRequestOperation * operation, id responseObject) {MyLog (@ "% @", operation. responseString); nsange range = [operation. responseString rangeOfString: @ "\" msg \ ": \" 0 \ ""]; if (range. location! = NSNotFound) {isTrue = true;} if (! IsTrue) {SHOWALERT (@ "error", @ "you need to contact the developer") ;}} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {MyLog (@ "Return failure result: % @", error. localizedFailureReason); SHOWALERT (@ "error", @ "failed to request a developer server"); isTrue = true ;}]; return isTrue ;}

Synchronous request:

-(BOOL) getOnlyKey2 {NSString * myUUIDStr = [[UIDevice currentDevice] identifierForVendor] UUIDString]; BOOL isTrue = false; NSString * urlstr = [NSString stringWithFormat: @ "http: // Timeout: 28080/try/check "]; NSURL * url = [NSURL URLWithString: urlstr]; NSMutableURLRequest * urlrequest = [[NSMutableURLRequest alloc] initWithURL: url]; urlrequest. HTTPMethod = @ "POST"; NSString * bodyStr = [NSString str IngWithFormat: @ "imei = % @ & av = % @", myuidstr, AppVersion]; NSData * body = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; urlrequest. HTTPBody = body; AFHTTPRequestOperation * requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest: urlrequest]; requestOperation. responseSerializer. acceptableContentTypes = [NSSet setWithObject: @ "text/plain"]; [requestOperation start]; [requestOperation waitUn TilFinished]; MyLog (@ "% @", requestOperation. responseString); nsange range = [requestOperation. responseString rangeOfString: @ "\" msg \ ": \" 0 \ ""]; if (range. location! = NSNotFound) {isTrue = true;} if (! IsTrue) {SHOWALERT (@ "error", @ "you need to contact the developer");} return isTrue ;}

Original Ecological synchronization request:

-(BOOL) getOnlyKey {NSString * myUUIDStr = [[UIDevice currentDevice] identifierForVendor] UUIDString]; // application version number NSDictionary * infoDict = [[NSBundle mainBundle] infoDictionary]; NSString * versionNum = [infoDict objectForKey: @ "CFBundleVersion"]; NSString * urlString = [NSString stringWithFormat: @ "http: // 122.225.89.70: 28080/try/check"]; NSURL * url = [NSURL URLWithString: urlString]; NSMutableURLRequest * req Uest = [NSMutableURLRequest requestWithURL: url]; [request setHTTPMethod: @ "POST"]; NSString * bodyStr = [NSString stringWithFormat: @ "imei =%@ & av =% @", myUUIDStr, versionNum]; // convert nstring to nsdata NSData * body = [bodyStr dataUsingEncoding: NSUTF8StringEncoding]; // MyLog (@ "body data % @", body ); [request setHTTPBody: body]; NSURLResponse * response = nil; NSError * error = nil; // second, the three parameters are pointer to the pointer, and all parameters must use the accessors, this method is used by the synchronization party. Method. The synchronization operation is not completed, and subsequent code will not be executed. NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error]; // NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; // MyLog (@ "returned result: % @", str); if (error = nil) {// receives data, NSString * str = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; MyLog (@ "% @", str); nsange range = [str rangeOfString: @ "\" msg \": \ "0 \" "]; if (range. location! = NSNotFound) {return true;} else {return false; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "error bird" message: @ "you need to contact the project developer" delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil]; [alert show] ;}} if (error! = Nil | response = nil) {return false; UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "error" message: @ "login failed, network instability "delegate: nil cancelButtonTitle: @" OK "otherButtonTitles: nil]; [alert show];} return false ;}


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.