Snail—iOS網路學習之得到網路上的資料

來源:互聯網
上載者:User

標籤:with   sign   網路學習   atom   工具   error   led   data   就會   

在開發項目project中,尤其是手機APP,一般都是先把介面給搭建出來。然後再從網上down資料 來填充

那麼網上的資料是怎麼得來的呢,網路上的資料無非就經常使用的兩種JSON和XML

如今 大部分都是在用JSON

網路上資料轉送都是以二進位形式進行傳輸的 ,僅僅要我們得到網上的位元據

假設它是JSON的二進位形式 那麼我們就能夠用JSON進行解析 假設是XML。那麼我們能夠用XML解析

關鍵是怎麼得到網上的位元據呢

設計一個經常使用的工具類 非常easy 給我一個介面(URL),那我就能夠用這個類得到二進位檔案

建立了一個類WJJHttpReques 繼承NSObject

以下是.h的代碼

#import <Foundation/Foundation.h>@interface WJJHttpRequest : NSObject//請求的介面@property (nonatomic,copy) NSString * httpUrl;//網上下載的二進位檔案@property (nonatomic,strong) NSMutableData * data;//代理@property (nonatomic,strong) id delegate;//代理的方法@property (nonatomic,assign) SEL method;//開始下載資料- (void)start;//中斷連線- (void)stop;@end

#import "WJJHttpRequest.h"#import "WJJRequestManager.h"@interface WJJHttpRequest ()<NSURLConnectionDataDelegate>{    //聲明connection為全域變數    NSURLConnection * _connection;}@end@implementation WJJHttpRequest//開始下載資料- (void)start{    NSURL * url = [NSURL URLWithString:self.httpUrl];    NSURLRequest * request = [[NSURLRequest alloc] initWithURL:url];    //僅僅要以下運行 那麼代理方法就會運行了 然後開始從網上down資料    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];}#pragma mark NSURLConnectionDataDelegate method//收到server的響應調用的代理方法- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSLog(@"收到server響應");    if (self.data == nil) {        self.data = [[NSMutableData alloc] init];    }else{        [self.data setLength:0];    }}//接受server的二進位檔案- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    NSLog(@"接受到了server的位元據");    [self.data appendData:data];}//假設成功了 參數就是YES 反之則是NO- (void)loadFinished:(BOOL)success{    if (!success) {        [self.data setLength:0];    }    //檢測要接收資料的回調對象 是否有method這種方法    if ([self.delegate respondsToSelector:self.method]) {        //假設有就運行這種方法 而且把自己當參數傳過去        [self.delegate performSelector:self.method withObject:self];    }    //這個是我自己設計的Request管理類 以下這句話的意思就是把 資料傳給那些須要資料的地方後,把這個串連斷開    [[WJJRequestManager sharedManager] removeTask:self.httpUrl];}//接受資料完畢時調用的方法- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSLog(@"資料接受完畢");    [self loadFinished:YES];}//接收資料失敗時調用的方法- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"資料請求失敗");    [self loadFinished:NO];}//停止下載資料- (void)stop{    if (_connection) {        //取消串連        [_connection cancel];    }    _connection = nil;}@end


Snail—iOS網路學習之得到網路上的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.