Snail-ios Network Learning to get the data on the network

Source: Internet
Author: User

In development projects, especially mobile apps, the interface is usually built first, and then from the online down data to fill

So how does the data on the internet come from, the data on the network is nothing more than the two commonly used JSON and XML

Most of them are now using JSON

Transmission of data on the network is transmitted in binary form, as long as we get the binary data on the Web

If it is a binary form of JSON then we can parse it with JSON if it is XML, then we can parse it with XML

The key is how to get binary data on the web.

Design a common tool class very simply give me an interface (URL), then I can use this class to get a binary file

A new class Wjjhttpreques inherited NSObject

Here's the code for. h

#import <Foundation/Foundation.h> @interface wjjhttprequest:nsobject//Request Interface @property (nonatomic,copy) NSString * httpurl;//Web-downloaded binaries @property (nonatomic,strong) nsmutabledata * data;//agent @property (Nonatomic,strong) ID delegate;//Proxy Method @property (nonatomic,assign) SEL method;//start download data-(void) start;//disconnect-(void) stop; @end

#import "WJJHttpRequest.h" #import "WJJRequestManager.h" @interface wjjhttprequest () <nsurlconnectiondatadelegate >{//Declare connection as global variable nsurlconnection * _connection;}    @end @implementation wjjhttprequest//Start downloading data-(void) start{nsurl * url = [Nsurl URLWithString:self.httpUrl];    Nsurlrequest * request = [[Nsurlrequest alloc] initwithurl:url]; As soon as the following executes then the proxy method executes and then starts from the online down data _connection = [[Nsurlconnection alloc] initwithrequest:request delegate:self];} #pragma mark Nsurlconnectiondatadelegate method//receives the proxy method of the server's response call-(void) connection: (Nsurlconnection *) connection    Didreceiveresponse: (Nsurlresponse *) response{NSLog (@ "received server response");    if (Self.data = = nil) {self.data = [[Nsmutabledata alloc] init];    }else{[Self.data setlength:0]; }}//accepts the server's binary file-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{NSLog (@ "    Received the binary data of the server "); [Self.data Appenddata:data];} If successful, the argument is yes and the no-(void) loadfinished: (BOOL) success{if (!success) {[Self.data setlength:0];         }//detects if the callback object to receive the data has method ([Self.delegate RespondsToSelector:self.method]) {//If any, execute this method and pass it on as a parameter.    [Self.delegate PerformSelector:self.method withobject:self]; }//This is my own design request management class The following sentence means to pass the data to those where the data is needed, disconnect the connection [[Wjjrequestmanager Sharedmanager] RemoveTask:self.httpUr L];}    method to be called when data completion is accepted-(void) connectiondidfinishloading: (nsurlconnection *) connection{NSLog (@ "Data accept complete"); [Self loadfinished:yes];}    Method called when receiving data failed-(void) connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) error{NSLog (@ "Data request failed"); [Self loadfinished:no];}    Stop download data-(void) stop{if (_connection) {//Cancel connection [_connection Cancel]; } _connection = nil;} @end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Snail-ios Network Learning to get the data on the network

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.