中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

IOS中強大的網(wǎng)絡通信類庫AFNetworking使用示例

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

AFNetworking提供了非常強大而實用的網(wǎng)絡操作API,結合使用NSURL和NSRequest來實現(xiàn)具體的網(wǎng)絡操作。

1,Objective-C版本:

以獲取twitter的一個接口文件為例

#import <AFNetworking/AFNetworking.h>

NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"];

NSURLRequest *request = [NSURLRequest requestWithURL: url];

// 使用AFJSONRequestOperation用于處理JSON格式的response數(shù)據(jù)。

// 同理還有AFXMLRequestOperation,AFPropertyListRequestOperation,

// AFImageRequestOperation等,都是AFHTTPRequestOperation的子類。

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request

    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSLog(@"Public Timeline: %@", JSON);

    } failure:nil];

[operation start];

說實話,IOS中的寫法真是繁瑣,不過字面意思確實表達足夠精確。


2,Swift中

若要使用AFNetworking,可將AFNetworking的目錄拖至項目中。在項目的工程中建立一個Bridging-Header.h頭文件,其中#import “AFNetworking/AFNetworking.h”,

然后在工程的build settings里邊尋找bridging,添加指定的bridge文件Bridging-Header.h,即在Swift工程中導入了OC的類庫。

下邊的例子,根據(jù)latitude和longitude來獲取天氣信息

fun updateWeatherInfo(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {

    let manager = AFHTTPRequestOperationManager()

    let url = "http://api.openweathermap.org/data/2.5/weather"

    let params = ["lat": latitude, "lon": longitude, "cnt": 0]

    manager.GET(url,

            parameters: params,

            success: { (operation: AFHTTPRequestOperation!,

                          responseObject: AnyObject!) in

                          println("JSON: " + responseObject.description!)

                          self.updateUISuccess(responseObject as NSDictionary!)

            },

            failure: { (operation: AFHTTPRequestOperation!,

                          error: NSError!) in

                          println("Error: " + error.localizedDescription)

                          self.loading.text = "Internet appears down!"

            }

    )

}


標簽: seo 網(wǎng)絡

版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:C#自定義的針對URL地址的處理類

下一篇:C#對asp.net的session進行簡單的封裝類