douhuang7263
2016-11-30 14:48
采纳率: 100%
浏览 33
已采纳

查看JSON feed Swift 3.0

I am trying to view my php result in an IOS app. The code is the following:

 func get()
    {
        //let url = NSURL(string: "http://www.w3schools.com/js/customers_mysql.php")
        let url = NSURL(string: "http://www.helpmewastetime.com/service.php")
        let data = NSData(contentsOf: url! as URL)

        values = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray


        var array = [""]
        array = (values.value(forKey: "Name") as? [String])!
        lb_Value.text = array[0]


    }

The code is working fine for the w3schools link, however it crashes when I use my link (hosted on go daddy) If you open the links from a web browser, we get the intended result.

This is the error received:

2016-11-30 16:44:24.977781 Fun iOS App[2590:112849] [] __nw_connection_get_connected_socket_block_invoke 2 Connection has no connected handler
2016-11-30 16:44:24.979616 Fun iOS App[2590:112846] PAC stream failed with
2016-11-30 16:44:24.981519 Fun iOS App[2590:112849] [] nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNetwork: 2
2016-11-30 16:44:26.145018 Fun iOS App[2590:112849] [] nw_endpoint_handler_add_write_request [1.1.1 107.180.54.250:80 failed socket-flow (satisfied)] cannot accept write requests
2016-11-30 16:44:26.145764 Fun iOS App[2590:112847] [] __tcp_connection_write_eof_block_invoke Write close callback received error: [22] Invalid argument
2016-11-30 16:44:26.150620 Fun iOS App[2590:112846] [] nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNetwork: 2
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift, line 178

Any help would be much appreciated

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在IOS应用程序中查看我的php结果。 代码如下:

  func get()
 {
 // let url = NSURL(string:“http://www.w3schools.com/js/customers_mysql.php")nn url url  = NSURL(字符串:“http://www.helpmewastetime.com/service.php")nn let data = NSData(contentsOf:url!as URL)
 
 values = try!  JSONSerialization.jsonObject(with:data!as Data,options:JSONSerialization.ReadingOptions.allowFragments)as!  NSArray 
 
 
 var array = [“”] 
 array =(values.value(forKey:“Name”)as?[String])!
 lb_Value.text = array [0] 
 
  
} 
   
 
 

代码在w3schools链接上工作正常,但是当我使用我的链接时(托管在爸爸身上)它会崩溃 如果你打开链接 从网络浏览器中,我们得到了预期的结果。

这是收到的错误:

  2016-11-30 16:44  :24.977781有趣的iOS应用程序[2590:112849] [] __nw_connection_get_connected_socket_block_invoke 2连接没有连接处理程序
2016-11-30 16:44:24.979616有趣的iOS应用程序[2590:112846] PAC流失败了
2016-11-30 16:  44:24.981519有趣的iOS应用[2590:112849] [] nw_proxy_resolver_create_parsed_array PAC评估错误:kCFErrorDomainCFNetwork:2 
2016-11-30 16:44:26.145018有趣的iOS应用[2590:112849] [] nw_endpoint_handler_add_write_request [1.1.1 107.180.54.250  :80 socket socket-failed(满意)]无法接受写请求
2016-11-30 16:44:26.145764有趣的iOS App [2590:112847] [] _  _tcp_connection_write_eof_block_invoke写入关闭回调收到错误:[22]无效参数
2016-11-30 16:44:26.150620有趣的iOS应用程序[2590:112846] [] nw_proxy_resolver_create_parsed_array PAC评估错误:kCFErrorDomainCFNetwork:2 
fatal错误:'try!' 表达式意外地引发了错误:错误Domain = NSCocoaErrorDomain Code = 3840“字符0周围的值无效”  UserInfo = {NSDebugDescription =字符0周围的值无效}}:file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.58.6/src/swift/stdlib/public/core/ErrorType.swift,line  178 
   
 
 

非常感谢任何帮助

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqiang2024 2016-11-30 15:14
    已采纳

    As Eric Aya mentioned in his comment. The issue is that your PHP service spits out the JSON data into an HTML page.

    When I view source of your page I get the following:

    <html>
    [{"ID":"1","Name":"Peter"},{"ID":"2","Name":"James"}]
    </html>
    

    The page source of the w3schools url is simply JSON. In your PHP service, output your data in JSON and you should be good to go.

    点赞 评论

相关推荐 更多相似问题