iOS Development-http Request

Source: Internet
Author: User

  • What is a URL?
    URLs are the addresses and locations of resources, and each resource on the Internet has a unique URL
    Basic format of URL:


  • Common protocols in URLs

    (1) HTTP

    Hypertext Transfer Protocol, access to remote network resources, the format is HTTP//

    HTTP protocol is the most commonly used protocol in network development.

    (2) file

    Access to resources on the local computer in the format file://(no host address)

    (3) Mailto

    The email address is accessed in the format mailto:

    (4) FTP

    The file resource for the shared host is accessed in the format ftp://

  • HTTP request example, return JSON
        //1. Stitching a request path with the request parameters followed by the URL? Separated by A & symbol between multiple parametersNSString *httpurl = [NSString stringWithFormat:@"https://free-api.heweather.com/v5/weather?city=%@&key=xxx", [CityName stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]];
    //2. Create a requestNsurlrequest *request =[Nsurlrequest Requestwithurl:[nsurl Urlwithstring:httpurl]; //3. Place the requested URL data in the NSData object (synchronous request, download data on the main thread, execution time varies according to data size)NSData *response =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; //4. Parse the data, according to the type of the request return, here with IOS5 parse class nsjsonserialization from response to parse out the data into the dictionaryNsdictionary *jsondict = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves Error: NIL];

    Asynchronous request

     //  3. Send an asynchronous request, return the data back to the main thread to perform the task  [ Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, nserror * _nullable Connectione  Rror) { if  (connectionerror | | data == nil) {NSLog ( @ "  The network is busy, please try   " );  //  4. Parse the data, depending on the type returned by the request, Here, we use IOS5 to parse the data from response into the dictionary nsjsonserialization the  nsdictionary *jsondict = [    Nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; }];

  • HTTP request returns XML

    First, XML simple introduction

    XML: Full name is extensible Markup Language, translated "Extensible Markup Language"

    Like JSON, it is also a commonly used data format for interaction, often called an XML document (XML documents).

    XML Example

    <videos>

    <video name= "Little yellow No. 01" length= "/>"

    <video name= "Small yellow No. 02" length= "/>"

    <video name= "Small yellow No. 03" length= "/>"

    </videos>

    II. syntax of XML

    1. Brief description

    A common XML document typically consists of the following sections

    Document Declaration

    Elements (Element)

    Properties (Attribute)

    2. Document Declaration

    At the very front of the XML document, you must write a document declaration that declares the type of the XML document

    The simplest statement

    <?xml version= "1.0"?>

    Use the Encoding property to describe the character encoding of a document

    <?xml version= "1.0" encoding= "UTF-8"?>

    3.XML syntax – elements (Element)

    An element includes a start tag and an end tag

    own element content:<video> Yellow man </video>

    No element content:<video></video>

    Shorthand:<video/> with no element content

    An element can nest several child elements (no cross-nesting)

    <videos>

    <video>

    <name> Small Yellow No. 01 Department </name>

    <length>30</length>

    </video>

    </videos>

    Attention:

    (1) The canonical XML document has a maximum of 1 root elements, and the other elements are descendants of the root element

    (2) All spaces and line breaks in XML are treated as concrete content

    The contents of the following two elements are not the same

    1th One

    <video> Small yellow people </video>

    2nd One

    <video>

    Minions

    </video>

    4.XML syntax – attributes (Attribute)

    An element can have multiple properties

    <video name= "Little yellow No. 01" length= "/>---->video element has name and length two properties

    Note: Attribute values must be enclosed in double quotation marks "" or single quotes "

    In fact, the information represented by a property can also be represented by a child element, such as

    <video>

    <name> Small Yellow No. 01 Department </name>

    <length>30</length>

    </video>

    Third, the parsing of XML

    1. Brief description

    To extract useful information from XML, you have to learn to parse the XML

    Extract the contents of the name element

    <name> Small Yellow No. 01 Department </name>

    Extract the value of the name and length property in the video element

    <video name= "Little yellow No. 01" length= "/>"

    There are 2 ways to parse XML

    (1) DOM: Loading the entire XML document into memory at once, which is more suitable for parsing small files

    (3) SAX: Starting from the root element, in order an element of an element down, more suitable for parsing large files

    Parsing of XML in 2.IOS development

    In iOS, there are many ways to parse XML

    (1) Apple native Nsxmlparser:sax method analysis, easy to use

    (2) Third party framework

    1) LIBXML2: pure C language, which is included by default in the iOS SDK while supporting DOM and sax parsing

    2) Gdataxml:dom method Analysis, developed by Google, based on LIBXML2

    Suggestions for choosing XML parsing methods:

    Large files: Nsxmlparser, LIBXML2

    Small files: gdataxml

    Parsing XML data with Gdataxml



    Parsing with Nsxmlparser


    Proxy approach to implement <NSXMLParserDelegate>

iOS Development-http request

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.