AFN同步非同步請求,afn同步非同步

來源:互聯網
上載者:User

AFN同步非同步請求,afn同步非同步

非同步請求:

-(BOOL)getOnlyKey1{    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];        __block bool isTrue = false;        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager 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);        NSRange range = [operation.responseString rangeOfString:@"\"msg\":\"0\""];        if (range.location != NSNotFound) {            isTrue = true;        }        if (!isTrue) {            SHOWALERT(@"錯誤", @"您需要聯絡開發人員");        }            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {        MyLog(@"返回失敗結果:%@", error.localizedFailureReason);        SHOWALERT(@"錯誤", @"請求開發人員伺服器失敗");        isTrue = true;    }];    return  isTrue;}

同步請求:

-(BOOL)getOnlyKey2{    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];    BOOL isTrue = false;    NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];    NSURL *url = [NSURL URLWithString:urlstr];    NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url];    urlrequest.HTTPMethod = @"POST";    NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, 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 waitUntilFinished];    MyLog(@"%@",requestOperation.responseString);    NSRange range = [requestOperation.responseString rangeOfString:@"\"msg\":\"0\""];    if (range.location != NSNotFound) {        isTrue = true;    }    if (!isTrue) {        SHOWALERT(@"錯誤", @"您需要聯絡開發人員");    }    return  isTrue;}

原生態的同步請求:

-(BOOL)getOnlyKey{    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];        //應用版本號碼    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 *request = [NSMutableURLRequest requestWithURL:url];        [request setHTTPMethod:@"POST"];    NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, versionNum];    //將nstring轉換成nsdata    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];    //MyLog(@"body data %@", body);    [request setHTTPBody:body];    NSURLResponse *response = nil;    NSError *error = nil;    //第二,三個參數是指標的指標,所有要用取址符,這個方法是同步方法。同步操作沒有完成,後面的代碼不會執行。    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];        //    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];    //    MyLog(@"返回結果是:%@", str);        if (error == nil) {  //接受到資料,表示工作正常        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];        MyLog(@"%@",str);        NSRange range = [str rangeOfString:@"\"msg\":\"0\""];        if (range.location != NSNotFound) {            return true;        }else{            return false;            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出錯鳥"                                                            message:@"您需要聯絡項目開發人員"                                                           delegate:nil                                                  cancelButtonTitle:@"確定"                                                  otherButtonTitles:nil];            [alert show];        }    }        if(error != nil || response == nil)    {        return false;        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"錯誤"                                                        message:@"登陸失敗,網路不穩定"                                                       delegate:nil                                              cancelButtonTitle:@"確定"                                              otherButtonTitles:nil];        [alert show];                    }        return false;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.