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?
- Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];
- nsurlrequest *request = [nsurlrequest requestwithurl:url];
- afhttprequestoperation *operation1 = [[Afhttprequestoperation alloc] initwithrequest:request];
- [Operation1 setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID responseobject) {
- NSLog (@ "Response:%@", [[NSString alloc] initwithdata:responseobject encoding:nsutf 8StringEncoding]);
- } failure:^ (afhttprequestoperation *operation, nserror *error) {
- NSLog (@ "error:%@", error);
- }];
- Nsurl *url2 = [Nsurl urlwithstring:@ "http://www.sohu.com"];
- nsurlrequest *request2 = [Nsurlrequest requestwithurl:url2];
- afhttprequestoperation *operation2 = [[Afhttprequestoperation alloc] initwithrequest:request2];
- [Operation2 setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID Responseobject) {
- NSLog (@ "Response2:%@", [[NSString alloc] initwithdata:responseobject encoding:nsutf 8StringEncoding]);
- } failure:^ (afhttprequestoperation *operation, nserror *error) {
- NSLog (@ "error:%@", error);
- }];
- Nsurl *url3 = [Nsurl urlwithstring:@ "http://www.sina.com"];
- nsurlrequest *request3 = [nsurlrequest requestwithurl:url3];
- afhttprequestoperation *operation3 = [[Afhttprequestoperation alloc] initwithrequest:request3];
- [Operation3 setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, ID responseobject) {
- NSLog (@ "RESPONSE3:%@", [[NSString alloc] initwithdata:responseobject encoding:nsutf 8StringEncoding]);
- } failure:^ (afhttprequestoperation *operation, nserror *error) {
- NSLog (@ "error:%@", error);
- }];
- //Simultaneous request
- nsoperationqueue *operationqueue = [[Nsoperationqueue alloc] init];
- [Operationqueue Setmaxconcurrentoperationcount:3];
- [Operationqueue addoperations:@[operation1, Operation2, Operation3] waituntilfinished:NO];
- //operation2 execution After operation1 request is completed
- nsoperationqueue *operationqueue = [[Nsoperationqueue alloc] init];
- [Operation2 adddependency:operation1];
- [Operationqueue addoperations:@[operation1, Operation2, Operation3] waituntilfinished:NO];
Below is the POST request:
[OBJC]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
- Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@ "https:// gowalla.com/friendships/request?user_id=1699 "];
- [Request Sethttpmethod:@ "POST"];
- Nsdictionary *headers = [nsdictionary dictionarywithobject:[nsstring stringwithformat:@ "Token token=\"%@ \ "", Koauthtoken] forkey:@ "Authorization"];
- [Request Setallhttpheaderfields:headers];
- Afhttprequestoperation *operation = [afhttprequestoperation operationwithrequest:request completion:^ ( Nsurlrequest *request, nshttpurlresponse *response, nsdata *data, nserror *error) {
- BOOL httpstatuscodeisacceptable = [[Nsindexset Indexsetwithindexesinrange:nsmakerange (200, 100)] [ containsindex:[response StatusCode]];
- if (httpstatuscodeisacceptable) {
- NSLog (@ "Friend Request Sent");
- } Else {
- NSLog (@ "[Error]: (%@%@)%@", [Request HttpMethod], [[Request URL] RelativePath], Error);
- }
- }];
- Nsoperationqueue *queue = [[[[Nsoperationqueue alloc] init] autorelease];
- [Queue addoperation:operation];
Afnetworking Queue Request