IOS-based ASIHttp simple network request implementation

Source: Internet
Author: User

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];

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.