[Swift通天遁地]四、网络和线程-(5)解析网络请求数据:String(字符串)、Data(二进制数据)和JSON数据

本文将演示如何解析网络请求数据:使用Alamofire的Get请求并输出字符串(String)、二进制数据(Data)和JSON数据。

首先确保在项目中已经安装了所需的第三方库。

点击【Podfile】,查看安装配置文件。

1 source 'https://github.com/CocoaPods/Specs.git'
2 platform :ios, '12.0'
3 use_frameworks!
4 
5 target ‘DemoApp’ do
6     pod 'Alamofire', '~> 4.0'
7 end

根据配置文件中的相关配置,安装第三方库。

然后点击打开【DemoApp.xcworkspace】项目文件。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,实现网络请求数据的解析功能。

  1 import UIKit
  2 //在当前的类文件中,引入已经安装的第三方类库
  3 import Alamofire
  4 
  5 class ViewController: UIViewController {
  6 
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10         //依次测试下文的四个解析服务器返回数据的方法
 11         //未知数据格式
 12         responseHandler()
 13 
 14         //字符串
 15         //responseStringHandler()
 16 
 17         //二进制
 18         //responseDataHandler()
 19 
 20         //JSON
 21         //responseJsonHandler()
 22     }
 23     
 24     //添加一个方法,处理无法明确服务器返回数据的格式的情况
 25     func responseHandler()
 26     {
 27         //调用网络操作库的网络请求方法,并处理从服务器返回的信息
 28         Alamofire.request("https://httpbin.org/get").response
 29         { 
 30             response in
 31             //在控制台输出:返回的网络请求对象
 32             print("Request: \(response.request)")
 33             //在控制台输出:网络返回对象
 34             print("Response: \(response.response)")
 35             //在控制台输出:错误信息
 36             print("Error: \(response.error)")
 37             
 38             //获得网络返回的数据,并对数据进行字符编码
 39             if let data = response.data, let utf8Text = String(data: data, encoding: .utf8)
 40             {
 41                 //在控制台输出编码后的内容
 42                 print("Data: \(utf8Text)")
 43             }
 44         }
 45     }
 46     
 47     //添加一个方法,用来解析由服务器返回的字符串数据
 48     func responseStringHandler()
 49     {
 50         //调用网络操作库的网络请求方法,并处理从服务器返回的字符串数据
 51         Alamofire.request("https://httpbin.org/get").responseString
 52         { 
 53             response in
 54             //在控制台输出:网络请求是否成功
 55             print("Success: \(response.result.isSuccess)")
 56             //在控制台输出:网络返回结果的值
 57             print("Response String: \(response.result.value)")
 58         }
 59     }
 60     
 61     //添加一个方法,用来解析由服务器返回的二进制数据
 62     func responseDataHandler()
 63     {
 64         //调用网络操作库的网络请求方法,并处理从服务器返回的二进制数据
 65         Alamofire.request("https://httpbin.org/get").responseData
 66         { 
 67             response in
 68             //在控制台输出返回对象的详细信息
 69             debugPrint("All Response Info: \(response)")
 70             
 71             //获得网络返回的数据,并对数据进行字符编码
 72             if let data = response.result.value, let utf8Text = String(data: data, encoding: .utf8)
 73             {
 74                 //在控制台输出编码后的内容
 75                 print("Data: \(utf8Text)")
 76             }
 77         }
 78     }
 79     
 80     //添加一个方法,用来解析由服务器返回的JSON数据
 81     func responseJsonHandler()
 82     {
 83         //调用网络操作库的网络请求方法,并处理从服务器返回的JSON数据
 84         Alamofire.request("https://httpbin.org/get").responseJSON
 85         { 
 86             response in
 87             //在控制台输出返回对象的详细信息
 88             debugPrint(response)
 89             
 90             //获得网络返回的数据,并对数据进行字符编码
 91             if let json = response.result.value
 92             {
 93                 //在控制台输出编码后的内容
 94                 print("JSON: \(json)")
 95             }
 96         }
 97     }
 98     
 99     override func didReceiveMemoryWarning() {
100         super.didReceiveMemoryWarning()
101         // Dispose of any resources that can be recreated.
102     }
103 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10222383.html