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 ;}