Starting from This article, Antioch is ready to further introduce the actual application of the ASIHTTPRequest framework for downloading data and uploading data.
In order to realize multi-threaded concurrent request network capability, ASIHTTPRequest is designed to be nsoperation subclass. Asinetworkqueue is designed as a subclass of Nsopertaionqueue. If Nsopertaionqueue is a thread manager, Nsoperation is the equivalent of a thread. They are added to the nsoperationqueue queue for orderly execution. Asinetworkqueue and ASIHTTPRequest also have the same concept, except that Asinetworkqueue thread Manager provides more network-related services, such as getting the download progress. Therefore, the following Antioch is mainly about the Asinetworkqueue management request queue demo. Below, please follow Antioch to see an example of the use of queues. When we click the Go button, we download two images from the server side and display them in the interface.
the complete source code for two of these files is as followsViewController.h, VIEWCONTROLLER.M
#import
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "Nsnumber+message.h"
#import "Nsstring+urlencoding.h"
@interface Viewcontroller:uiviewcontroller
@property (Weak, nonatomic) Iboutlet Uiimageview *imageview1;
@property (Weak, nonatomic) Iboutlet Uiimageview *imageview2;
@property (strong) Asinetworkqueue *networkqueue;
-(Ibaction) OnClick: (ID) sender;
@end
---------------------------------------------------------
#import "ViewController.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
}
-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
}
-(Ibaction) OnClick: (ID) Sender {
if (!_networkqueue) {
_networkqueue = [[Asinetworkqueue alloc] init];
}
Stop the previous queue
[_networkqueue cancelalloperations];
Create an ASI queue
[_networkqueue setdelegate:self];
[_networkqueue setrequestdidfinishselector: @selector (requestfinished:)];
[_networkqueue setrequestdidfailselector: @selector (requestfailed:)];
[_networkqueue setqueuedidfinishselector: @selector (queuefinished:)];
for (int i=1; I<<span style= "color: #272ad8" >3; i++) {
NSString *strurl = [[NSString alloc] initwithformat:@ "http://www.crazyit.com/service/download.php?email=%@& Filename=test%i.jpg "@" [email protected] ", I];
Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring];
ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
Request.tag = i;
[_networkqueue Addoperation:request];
}
[_networkqueue go];
}
-(void) requestfinished: (ASIHTTPRequest *) request
{
NSData *data = [request ResponseData];
NSLog (@ "data =%@", data);
Nserror *eror;
Nsdictionary *resdict = [Nsjsonserialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:& Eror];
If the server is the returned picture, that is through the data IO flow, the resdict value here must be NULL, because data is not in JSON format, so nsjsonserialization after processing can not be directly into the JSON format.
NSLog (@ "resdict =%@", resdict);
if (!resdict) {
UIImage *img = [UIImage imagewithdata:data];
if (Request.tag ==1) {
_imageview1.image = img;
} else {
_imageview2.image = img;
}
} else {
If the server is the returned picture, that is through the data IO flow, the resdict value here must be NULL, because data is not in JSON format, so nsjsonserialization after processing can not be directly into the JSON format.
If it is returned in JSON format, generally similar to {"ResultCode":-1} format, if the picture IO stream is returned, it must not be returned in JSON format
NSNumber *resultcodeobj = [resdict objectforkey:@ "ResultCode"];
NSString *errorstr = [Resultcodeobj errormessage];
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "error message"
Message:errorstr
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alertview show];
}
if ([_networkqueue requestscount] = = 0) {
[Self setnetworkqueue:nil];
}
NSLog (@ "request succeeded");
}
-(void) requestfailed: (ASIHTTPRequest *) request
{
Nserror *error = [request ERROR];
NSLog (@ "%@", [Error localizeddescription]);
if ([_networkqueue requestscount] = = 0) {
[Self setnetworkqueue:nil];
}
NSLog (@ "request failed");
}
-(void) queuefinished: (ASIHTTPRequest *) request
{
if ([_networkqueue requestscount] = = 0) {
[Self setnetworkqueue:nil];
}
NSLog (@ "queue complete");
}
@end
-------------------------
#import
@interface NSNumber (Message)
-(NSString *) errormessage;
@end
------------------------
#import "Nsnumber+message.h"
@implementation NSNumber (Message)
-(NSString *) errormessage
{
NSString *errorstr = @ "";
switch ([self integervalue]) {
Case-9:
ERRORSTR = @ "file is not specified. ";
Break
Case-8:
ERRORSTR = @ "File not found. ";
Break
Case-7:
Errorstr = @ "no data. ";
Break
Case-6:
Errorstr = @ "date not entered. ";
Break
Case-5:
Errorstr = @ "The content is not entered. ";
Break
Case-4:
ERRORSTR = @ "id not entered. ";
Break
Case-3:
Errorstr = @ "Failed to access the data. ";
Break
Case-2:
ERRORSTR = @ "Your account can insert up to 10 data. ";
Break
Case-1:
Errorstr = @ "The user does not exist, please register to http://www.crazyit.com." ";
Default
Break
}
return errorstr;
}
@end
ASIHTTPRequest Framework use Summary series of the Church Tutorial 4 (Download data)