Description:
ASIHttpRequest is a method used to apply a third-party library. It uses code quickly to reduce the amount of code and improve efficiency. preparation:
I. Import a third-party library ASIHttpRequest
2. Many errors will be reported for two reasons: one is to import the four files in Xcode, and the other is that it does not use automatic reference count.
Iii. solution 1. Import four system files: MobileCoreServices. framework SystemConfiguration. framework
CFNetwork. framework libz.1.2.5.dylib 2. manually disable compile sources in the buide phase of the Project
In, click the ASIHhttp type, and add-fno-objc-arc 3. Run the command again, so that no error is reported for implementing asynchronous post requests.
# Import "ASIHTTPRequest. h"
-(Void) viewDidLoad
{
[Self requestHttp];
}
-(Void) requestHttp
{
// 1200792098331
// Create a URL object first
NSURL * url = [NSURL urlWithString: [NSString stringWithFormat: @ "http://api.kuaidi100.com/api? Id = d6b9888b0da96f6b & com =%@ & nu =%@ & show = 0 & muti = 1 & order = desc & display = mobile ", name];
NSURL * url = [NSURL URLWithString: [NSString stringWithFormat: @ "http://api.kuaidi100.com/api? Id = d6b9888b0da96f6b & com =%@ & nu =%@ & show = 0 & muti = 1 & order = desc & display = mobile "];
NSLog (@ "url ======%@", url );
Create another request object
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
Send the request and return the corresponding JSon data
[Request setCompletionBlock:
^ {
NSError * error = nil;
// Create a dictionary to receive the returned JSON data
NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData: request. responseData options: kNilOptions error: & error];
NSLog (@ "% @", dictionary );
// Create an array and store the data in the dictionary in the array.
NSArray * arr = [dictionary objectForKey: @ "data"];
// Change the data in the array and store the changed data in a variable array.
For (NSDictionary * temp in arr)
{
[Self. array addObject: [NSString stringWithFormat: @ "% @ \ n % @", [temp objectForKey: @ "time"], [temp objectForKey: @ "context"];
}
// Assign the data in the variable array after the variant to a variable String object in the case of a string.
For (NSString * temp in self. array)
{
[Self. mutableString appendString: [NSString stringWithFormat: @ "% @ \ n", temp];
NSLog (@ "temp ===%@", temp );
}
// Here I assign the variable string after the variant to a lable
Self. jieGuoLale. text = self. mutableString;
NSLog (@ "self. jieGuoLale. text =====%@", self. jieGuoLale. text );
}];
// If the request fails to be sent, a warning box is displayed.
[Request setFailedBlock:
^ {
UIAlertView * alerteView = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "problem warning: the network connection is abnormal. Please try again later" delegate: nil cancelButtonTitle: @ "exit" otherButtonTitles: nil];
[AlerteView show];
}];
// Send an asynchronous request
[Request startAsynchronous];