正在 Swift 中检索网络字符串
Retrieving web string in Swift
当我使用 Swift post 到我的 Web 服务时,我很难检索数据。
我得到的只是返回的字符数,而不是实际字符数。我想他们一定在那里,我只是不知道如何找回他们。
这是我的 Swift 3 代码:
let url: NSURL = NSURL(string: "http://www.nanorig.dk/api/login")!
let request:NSMutableURLRequest = NSMutableURLRequest(url:url as URL)
let bodyData = "data=something"
request.httpMethod = "POST"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main)
{
(response, data, error) in
print("response: \(response)")
print("data: \(data)")
print("error: \(error)")
}
当我 运行 这段代码时,我得到以下输出:
response: Optional(<NSHTTPURLResponse: 0x174225400> { URL: http://www.nanorig.dk/api/login } { status code: 200, headers {
"Accept-Ranges" = bytes;
Age = 0;
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 85;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 06 Oct 2016 13:15:18 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
Pragma = "no-cache";
Server = Apache;
"Set-Cookie" = "PHPSESSID=6so30t6brvj6j5k03kihr1m1f5; path=/";
Vary = "Accept-Encoding";
Via = "1.1 varnish-v4";
"X-Powered-By" = "PHP/5.6.26";
"X-Varnish" = 159701416;
} })
data: Optional(45 bytes)
error: nil
2016-10-06 15:15:19.428231 NanOrig[2245:1192962] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2016-10-06 15:15:19.436932 NanOrig[2245:1192962] [MC] Reading from public effective user settings.
特别注意上面的下面一行:
data: Optional(45 bytes)
服务器上 运行 的方法是一个简单的 PHP 行:
echo 'just a test string to return to the swift app';
...这是 45 个字符...
所以,在我看来,"data" 变量应该以某种方式包含回显的内容;它还应该如何知道字符数?
我只是不知道如何把它们弄出来,我已经尝试了所有我能想到的方法(将方法调用放在变量中,将数据值分配给其他东西,将其转换为字符串,寻找 toString 方法,谷歌搜索并在这里搜索类似的问题,甚至尝试了与网络完全不同的代码片段,产生了同样的问题)。
我的应用程序中的其他地方有代码 returns 来自服务器的简单 GET 请求的字符串,但是,这需要是 POST - 我不是 post正在下载任何东西,因为它似乎不起作用。
真希望有人能告诉我这是怎么回事。
您应该能够使用 NSString(data: data, encoding: NSUTF8StringEncoding)
.
轻松地将数据转换为 NSString
要打开结果字符串(因为这是一个失败的初始化程序),请执行以下操作:
if let unwrappedDataString = dataString {
// Do things with the unwrapped string.
}
编辑:
为了完美地为您编写这篇文章,请使用以下内容。您的评论让您强制解包数据,这意味着如果出于某种原因数据不存在,您的应用程序将会崩溃。
if let unwrappedData = data {
if let dataString = NSString(data: unwrappedData, encoding: String.encoding.utf8.rawValue) {
// Do stuff
}
}
当我使用 Swift post 到我的 Web 服务时,我很难检索数据。 我得到的只是返回的字符数,而不是实际字符数。我想他们一定在那里,我只是不知道如何找回他们。
这是我的 Swift 3 代码:
let url: NSURL = NSURL(string: "http://www.nanorig.dk/api/login")!
let request:NSMutableURLRequest = NSMutableURLRequest(url:url as URL)
let bodyData = "data=something"
request.httpMethod = "POST"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main)
{
(response, data, error) in
print("response: \(response)")
print("data: \(data)")
print("error: \(error)")
}
当我 运行 这段代码时,我得到以下输出:
response: Optional(<NSHTTPURLResponse: 0x174225400> { URL: http://www.nanorig.dk/api/login } { status code: 200, headers {
"Accept-Ranges" = bytes;
Age = 0;
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 85;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 06 Oct 2016 13:15:18 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
Pragma = "no-cache";
Server = Apache;
"Set-Cookie" = "PHPSESSID=6so30t6brvj6j5k03kihr1m1f5; path=/";
Vary = "Accept-Encoding";
Via = "1.1 varnish-v4";
"X-Powered-By" = "PHP/5.6.26";
"X-Varnish" = 159701416;
} })
data: Optional(45 bytes)
error: nil
2016-10-06 15:15:19.428231 NanOrig[2245:1192962] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2016-10-06 15:15:19.436932 NanOrig[2245:1192962] [MC] Reading from public effective user settings.
特别注意上面的下面一行:
data: Optional(45 bytes)
服务器上 运行 的方法是一个简单的 PHP 行:
echo 'just a test string to return to the swift app';
...这是 45 个字符...
所以,在我看来,"data" 变量应该以某种方式包含回显的内容;它还应该如何知道字符数? 我只是不知道如何把它们弄出来,我已经尝试了所有我能想到的方法(将方法调用放在变量中,将数据值分配给其他东西,将其转换为字符串,寻找 toString 方法,谷歌搜索并在这里搜索类似的问题,甚至尝试了与网络完全不同的代码片段,产生了同样的问题)。
我的应用程序中的其他地方有代码 returns 来自服务器的简单 GET 请求的字符串,但是,这需要是 POST - 我不是 post正在下载任何东西,因为它似乎不起作用。
真希望有人能告诉我这是怎么回事。
您应该能够使用 NSString(data: data, encoding: NSUTF8StringEncoding)
.
NSString
要打开结果字符串(因为这是一个失败的初始化程序),请执行以下操作:
if let unwrappedDataString = dataString {
// Do things with the unwrapped string.
}
编辑:
为了完美地为您编写这篇文章,请使用以下内容。您的评论让您强制解包数据,这意味着如果出于某种原因数据不存在,您的应用程序将会崩溃。
if let unwrappedData = data {
if let dataString = NSString(data: unwrappedData, encoding: String.encoding.utf8.rawValue) {
// Do stuff
}
}