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

Source: Internet
Author: User

In project development projects, especially mobile apps, the interface is usually built first. And then populate it from the online down data

So how does the data on the web come from, the data on the web is nothing more than the two JSON and XML that are often used

Most of them are now using JSON

The data transmission on the network is transmitted in binary form, only we get the binary data on the net

Assuming that it's a binary form of JSON, then we can parse it with JSON, assuming it's XML. Then we can parse it with XML

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

Design a frequently used tool class very easy to give me an interface (URL), then I can use this class to get the binary file

A new class Wjjhttpreques inherited NSObject

The following is 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]; Just to run the proxy method will then run and start from the online down data _connection = [[Nsurlconnection alloc] initwithrequest:request delegate:self];} #pragma mark Nsurlconnectiondatadelegate method//receives a proxy method for the response call of the server-(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 binary file of the server-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *) data{NSLog (@ "    Received the binary data of the server "); [Self.data Appenddata:data];} Assuming the success of the parameter is yes the reverse is no-(void) loadfinished: (BOOL) success{if (!success) {[Self.data setlength:0];         }//Detect if the callback object to receive the data has method ([Self.delegate RespondsToSelector:self.method]) {//Dummy run this method and pass on your own as a number of references.    [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 called when the data is accepted-(void) connectiondidfinishloading: (nsurlconnection *) connection{NSLog (@ "Data accepted"); [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


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.