Afnetworking Queue Request

Source: Internet
Author: User

In the development process, we often encounter some pages more than one network request, sometimes need two three or more, this time we need a queue request, the bottom is a GET request of multiple requests placed in the queue inside:

[OBJC]View Plaincopyprint?
  1. Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
  2. nsurlrequest *request = [nsurlrequest requestwithurl:url];
  3. afhttprequestoperation *operation1 = [[Afhttprequestoperation alloc] initwithrequest:request];
  4. [Operation1 setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID responseobject) {
  5. NSLog (@ "Response:%@", [[NSString alloc] initwithdata:responseobject encoding:nsutf   8StringEncoding]);
  6. } failure:^ (afhttprequestoperation *operation, nserror *error) {
  7. NSLog (@ "error:%@", error);
  8. }];
  9. Nsurl *url2 = [Nsurl urlwithstring:@ "http://www.sohu.com"];
  10. nsurlrequest *request2 = [Nsurlrequest requestwithurl:url2];
  11. afhttprequestoperation *operation2 = [[Afhttprequestoperation alloc] initwithrequest:request2];
  12. [Operation2 setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID Responseobject) {
  13. NSLog (@ "Response2:%@", [[NSString alloc] initwithdata:responseobject encoding:nsutf   8StringEncoding]);
  14. } failure:^ (afhttprequestoperation *operation, nserror *error) {
  15. NSLog (@ "error:%@", error);
  16. }];
  17. Nsurl *url3 = [Nsurl urlwithstring:@ "http://www.sina.com"];
  18. nsurlrequest *request3 = [nsurlrequest requestwithurl:url3];
  19. afhttprequestoperation *operation3 = [[Afhttprequestoperation alloc] initwithrequest:request3];
  20. [Operation3 setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID responseobject) {
  21. NSLog (@ "RESPONSE3:%@", [[NSString alloc] initwithdata:responseobject encoding:nsutf   8StringEncoding]);
  22. } failure:^ (afhttprequestoperation *operation, nserror *error) {
  23. NSLog (@ "error:%@", error);
  24. }];
  25. //Simultaneous request
  26. nsoperationqueue *operationqueue = [[Nsoperationqueue alloc] init];
  27. [Operationqueue Setmaxconcurrentoperationcount:3];
  28. [Operationqueue addoperations:@[operation1, Operation2, Operation3] waituntilfinished:NO];
  29. //operation2 execution After operation1 request is completed
  30. nsoperationqueue *operationqueue = [[Nsoperationqueue alloc] init];
  31. [Operation2 adddependency:operation1];
  32. [Operationqueue addoperations:@[operation1, Operation2, Operation3] waituntilfinished:NO];

Below is the POST request:

[OBJC]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@ "https://  gowalla.com/friendships/request?user_id=1699 "];
    2. [Request Sethttpmethod:@ "POST"];
    3. Nsdictionary *headers = [nsdictionary dictionarywithobject:[nsstring stringwithformat:@ "Token token=\"%@  \ "", Koauthtoken] forkey:@ "Authorization"];
    4. [Request Setallhttpheaderfields:headers];
    5. Afhttprequestoperation *operation = [afhttprequestoperation operationwithrequest:request completion:^ ( Nsurlrequest *request, nshttpurlresponse *response, nsdata *data, nserror *error) {
    6. BOOL httpstatuscodeisacceptable = [[Nsindexset Indexsetwithindexesinrange:nsmakerange (200,   100)] [ containsindex:[response StatusCode]];
    7. if (httpstatuscodeisacceptable) {
    8. NSLog (@ "Friend Request Sent");
    9. } Else {
    10. NSLog (@ "[Error]: (%@%@)%@", [Request HttpMethod], [[Request URL] RelativePath], Error);
    11. }
    12. }];
    13. Nsoperationqueue *queue = [[[[Nsoperationqueue alloc] init] autorelease];
    14. [Queue addoperation:operation];

Afnetworking Queue Request

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.