1. Adding the ASI framework to the project
The 1>asi framework relies on reachability when judging the state of the network, it can be found in Apple's official documentation, or it can be found in the ASI sample example.
2> Add the class library that the ASI relies on: SystemConfiguration is the class library required by reachability
3> If the ARC project, also in Targets-compile Sources-compiler flags to the ASI's. m file, are set to-fno-objc-arc
4> finally compile the program to see whether the success
2. Initiating the request
2.1 Synchronization Requests
Set URL
Nsurl *url = [Nsurl urlwithstring:@ "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"];
Set Request
ASIHTTPRequest * request = [asihttprequest Requestwithurl:url];
Request.timeoutseconds = 10;
Initiating a request
[Request startsynchronous];
Get the data returned by the server
if (request.error) {
NSLog (@ "wrong");
}else{
NSLog (@ "Status code%d", Request.responsestatuscode);
NSLog (@ "Return information:%@", request.responsestring);
Nsdictionary *dict = [nsjsonserialization JSONObjectWithData:request.responseData options: Nsjsonreadingmutableleaves Error:nil];
NSLog (@ "dict------%@", dict);
}
2.2 Asynchronous requests
Set URL
Nsurl *url = [Nsurl urlwithstring:@ "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"];
Set Request
ASIHTTPRequest * request = [asihttprequest Requestwithurl:url];
Set up Proxy
Request.delegate = self;
Initiating a request
[Request startasynchronous];
#pragma mark-asihttprequestdelegate
-(void) requeststarted: (ASIHTTPRequest *) request{
NSLog (@ "start request");
}
-(void) Request: (ASIHTTPRequest *) Request Didreceivedata: (NSData *) data{
NSLog (@ "receiving data returned by the server"); //The method may be called multiple times because the data is to be stitched
}
-(void) requestfinished: (ASIHTTPRequest *) request{
NSLog (@ "request End");
NSData *data = Request.responsedata; //So you can get the final data for the request to complete
}
-(void) requestfailed: (ASIHTTPRequest *) request{
NSLog (@ "request failed");
}
Pit point for asynchronous requests with ASI:
Assuming that there should be a A, a, a, b two controller, the point a controller's navigation button jumps to the C controller. In the Controller B initiates an asynchronous request, the request is in progress (that is, the agent method Request:didreceivedata:), at which point the return key, return to the A controller, That is, the B controller is killed, press the above asynchronous request code will report bad memory access errors
Because request.delegate = b Controller, the B controller is executing [self request:xx didreceivedata:xx] When the request is in progress, and if the B controller is killed, the program executes the [self request:xx DIDRECEIVEDATA:XX] This method, can not find the B controller, will be reported bad memory access error.
The solution to this pit point is to remove the request in the B-controlled dealloc. The Blue code is the modified code
@property (Nonatomic,strong) asihttprequest *request;
Set URL
Nsurl *url = [Nsurl urlwithstring:@ "http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139"];
Set Request
self.request = [ASIHTTPRequest Requestwithurl:url];
Set up Proxy
self.request. Delegate = self;
Initiating a request
[self.request startasynchronous];
-(void) Dealloc {
[Self.request Cleardelegatesandcancel];
}
IOS asi--Synchronous and asynchronous requests