NSURLConnection

經常使用類ios

NSURL:請求地址json

 

NSURLRequest:一個NSURLRequest對象就表明一個請求,它包含的信息有緩存

一個NSURL對象服務器

請求方法、請求頭、請求體網絡

請求超時app

… …框架

 

NSMutableURLRequest:NSURLRequest的子類異步

 

NSURLConnectionurl

負責發送請求,創建客戶端和服務器的鏈接.net

發送NSURLRequest的數據給服務器,並收集來自服務器的響應數據

 

使用NSURLConnection發送請求的步驟很簡單

建立一個NSURL對象,設置請求路徑

傳入NSURL建立一個NSURLRequest對象,設置請求頭和請求體

使用NSURLConnection發送NSURLRequest

 

 

NSURLConnection常見的發送請求方法有如下幾種

同步請求

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;

 

異步請求:根據對服務器返回數據的處理方式的不一樣,又能夠分爲2種

block回調

+ (void)sendAsynchronousRequest:(NSURLRequest*) request                          queue:(NSOperationQueue*) queue                                                  completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler;

 

代理

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;

+ (NSURLConnection*)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate;

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately;

 

在startImmediately = NO的狀況下,須要調用start方法開始發送請求

- (void)start;

 

成爲NSURLConnection的代理,最好遵照NSURLConnectionDataDelegate協議

 

 

NSURLConnectionDataDelegate協議中的代理方法

開始接收到服務器的響應時調用

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

 

接收到服務器返回的數據時調用(服務器返回的數據比較大時會調用屢次)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

 

服務器返回的數據徹底接收完畢後調用

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

 

請求出錯時調用(好比請求超時)

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

 

NSMutableURLRequest是NSURLRequest的子類,經常使用方法有

設置請求超時等待時間(超過這個時間就算超時,請求失敗)

- (void)setTimeoutInterval:(NSTimeInterval)seconds;

 

設置請求方法(好比GET和POST)

- (void)setHTTPMethod:(NSString *)method;

 

設置請求體

- (void)setHTTPBody:(NSData *)data;

 

設置請求頭

- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

 

建立GET請求

NSString *urlStr = [@"http://192.168.1.102:8080/MJServer/login?username=123&pwd=123" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

 

建立POST請求

NSString *urlStr = @"http://192.168.1.102:8080/MJServer/login";

NSURL *url = [NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

request.HTTPMethod = @"POST";

// 請求體

NSString *bodyStr = @"username=123&pwd=123";

request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

 

 

如何發送JSON給服務器

必定要使用POST請求

設置請求頭

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

設置JSON數據爲請求體

 

 

在網絡應用中,須要對用戶設備的網絡狀態進行實時監控,目的是

讓用戶瞭解本身的網絡狀態,防止一些誤會(好比怪應用無能)

根據用戶的網絡狀態進行智能處理,節省用戶流量,提升用戶體驗

WIFI\3G網絡:自動下載高清圖片

低速網絡:只下載縮略圖

沒有網絡:只顯示離線的緩存數據

 

蘋果官方提供了一個叫Reachability的示例程序,便於開發者檢測網絡狀態

https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

 

Reachability的使用步驟

添加框架SystemConfiguration.framework

 

 

添加源代碼

 

包含頭文件

#import "Reachability.h"

 

常見用法

// 是否WIFI

+ (BOOL) IsEnableWIFI {

    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);

}

 

// 是否3G

+ (BOOL) IsEnable3G {

    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);

}

 

[[NSNotificationCenter defaultCenter] addObserver:self

 selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

self.netReachability = [Reachability reachabilityForInternetConnection];

[self.netReachability startNotifier];

 

- (void)dealloc

{

    [self.netReachability stopNotifier];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];

}

相關文章
相關標籤/搜索