Getting Started with iOS Network programming: ASIHTTPRequest Framework sync Request

Source: Internet
Author: User
Tags html form http request json

The classes associated with HTTP requests in the ASIHTTPRequest framework are: ASIHTTPRequest and asiformdatarequest, the most common of which is asihttprequest, Asiformdatarequest is a subclass of ASIHTTPRequest, Asiformdatarequest can send similar HTML form data, or upload data, by default the POST request method. Other HTTP request methods are also available. All of them can be asynchronous or synchronous requests. Below we introduce from the simplest get sync request.

1. Implement Get sync Request

Implementing a Get synchronization request uses the most basic request Class asihttprequest. We still use the Mynotes application as an example, only consider the query function implementation, modify the main view controller masterviewcontroller.m Startrequest method is as follows:

-(void) startrequest 
     
{ 
     
NSString *strurl = [[NSString alloc] Initwithformat: 
     
@ "http://iosbook3/mynotes/ webservice.php?email=%@&type=%@&action=%@ ", 
     
@" < your iosbook1.com user email > ", @" JSON ", @" Query "]; 
     
Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]]; 
     
ASIHTTPRequest *request = [ASIHTTPRequest requestwithurl:url]; 
     
[Request startsynchronous]; 
     
NSLog (@ "request complete ..."); 
     
Nserror *error = [request ERROR]; 
     
if (!error) { 
     
//nsstring *response = [request responsestring]; 
     
NSData *data  = [request ResponseData]; 
     
Nsdictionary *resdict = [nsjsonserialization jsonobjectwithdata:data 
     
options:nsjsonreadingallowfragments Error : nil]; 
     
[Self reloadview:resdict]; 
     
} 
     

Using the ASIHTTPRequest class, you need to introduce header file ASIHTTPRequest.h.

2, the implementation of post synchronization request

The

sends a request for a post method, both synchronous and asynchronous, using the Asiformdatarequest class, except that they are slightly different. In this section we only describe the use of the Asiformdatarequest class to send post methods under synchronization requests. The Startrequest method for modifying the main view controller MASTERVIEWCONTROLLER.M is as follows:

-(void) startrequest 
     
{ 
     
NSString *strurl = @ "http://iosbook3/mynotes/webservice.php"; 
     
Nsurl *url = [Nsurl urlwithstring:[strurl urlencodedstring]]; 
     
      
     
Asiformdatarequest *request = [Asiformdatarequest requestwithurl:url]; 
     
[Request setpostvalue:@ "< your iosbook1.com user email >" forkey:@ "email"];                ③ 
     
[Request setpostvalue:@ ' JSON ' forkey:@ ' type ']; 
     
[Request setpostvalue:@ "query" forkey:@ "action"]; 
     
[Request startsynchronous]; 
     
NSLog (@ "request complete ..."); 
     
Nserror *error = [request ERROR]; 
     
if (!error) { 
     
//nsstring *response = [request responsestring]; 
     
NSData *data  = [request ResponseData]; 
     
Nsdictionary *resdict = [nsjsonserialization jsonobjectwithdata:data 
     
options:nsjsonreadingallowfragments Error : nil]; 
     
[Self reloadview:resdict]; 
     
} 
     

The use of Asiformdatarequest requires the introduction of header file ASIFormDataRequest.h.

If we want to send a request method other than Get and post, you can use the [request setrequestmethod:@ ' put] statement, where put is the request method.

This article is from the "Dongsheng" blog, please be sure to keep this source http://2009315319.blog.51cto.com/701759/1168240

Related Article

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.