【问题标题】:HTTP Get Request with Alamofire使用 Alamofire 获取 HTTP 请求
【发布时间】:2020-12-13 19:26:41
【问题描述】:

我试图用 alamofire 做 Http Get 请求,但不知何故我得到了这个错误。我也使用 AlamofireObjectMapper 进行映射。但是我在哪里犯错?

这是我的代码:

class Networking  {

    static var sessionManager : SessionManager = {

        let config = URLSessionConfiguration.default

        config.timeoutIntervalForRequest = 120
        config.timeoutIntervalForResource = 120

        let manager = Alamofire.SessionManager(configuration: config)

        return manager

    }()


    static var header : [String : String] {

        get  {
            return [

                "Content-Type" : "application/json"

            ]

        }

    }

    static func getMainIndex() -> DataRequest {

        var urlComponents = URLComponents()

        urlComponents.scheme = "https"
        urlComponents.host = "unitedshopapp.tsoft.club/mobile-app"
        urlComponents.path = URLPath.mainIndex

        print("\n\n\n\(urlComponents.string!) Sending Request .......\n\n\n")

        return Networking.sessionManager.request(urlComponents.string!, method: .get ,encoding: JSONEncoding.default,headers: header)
    }
}

【问题讨论】:

    标签: swift alamofire


    【解决方案1】:

    urlComponents.host = "unitedshopapp.tsoft.club/mobile-app" 看起来不对 - “mobile-app”不是主机名的一部分,而是路径的一部分,应该移到下面的行:

    urlComponents.scheme = "https"
    urlComponents.host = "unitedshopapp.tsoft.club"
    urlComponents.path = "mobile-app/" + URLPath.mainIndex
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多