|
/**
* ASI's Get asynchronous request
* Note: The ASI monitoring request process is delegate, block and selector three ways,
* The test found that delegate and block can be executed simultaneously, selector and block can be executed simultaneously, but delegate and selector coexist (including three coexistence) do not perform delegate
* Execution order: Delegate > Selector > block
* Data not available through ResponseData and responsestring
* You cannot get data directly from ResponseData and responsestring using a proxy
*/
-(void) asigetasynch{
Get URL
Nsurl *url = [[Nsurl alloc] initwithstring:@ "Http://localhost:8070/MJServer/video"];
Get ASI Request Object
ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];
Set up the Asihttprequestdelegate agent.
Request.delegate = self;
Sending an asynchronous request
[Request startasynchronous];
Block to set the listening request process
[Request setstartedblock:^{
NSLog (@ "block request Start");
}];
[Request setdatareceivedblock:^ (NSData *data) {
NSLog (@ "Block gets request data--%@", [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error: Nil]);
}];
[Request setcompletionblock:^{
NSLog (@ "block request End");
}];
Set the selector of the listener request process
[Request Setdidstartselector: @selector (start:)];
[Request Setdidreceivedataselector: @selector (receivedata:)];
[Request Setdidfinishselector: @selector (finish:)];
}
#pragma mark Selector Monitoring method
-(void) Start: (ASIHTTPRequest *) request{
NSLog (@ "selector request start");
}
-(void) Receivedata: (ASIHTTPRequest *) request{
The data is not available here
NSLog (@ "Selector obtain request data-%@", [request responsestring]);
}
-(void) Finish: (ASIHTTPRequest *) request{
NSLog (@ "selector request End");
}
#pragma mark implements the proxy method for Asihttprequestdelegate
-(void) Request: (ASIHTTPRequest *) Request Didreceivedata: (NSData *) data{
NSLog (@ "Delegate GET request data-%@", [Nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]);
}
-(void) requeststarted: (ASIHTTPRequest *) request{
NSLog (@ "Delegate request start");
}
-(void) requestfinished: (ASIHTTPRequest *) request{
NSLog (@ "Delegate request End");
}
-(void) requestfailed: (ASIHTTPRequest *) request{
NSLog (@ "Delegate request failed");
}
|