【问题标题】:Alamofire Swift convertAlamofire Swift 转换
【发布时间】:2018-04-26 08:07:32
【问题描述】:

您好,我想向 API 发出请求,但是发送时,控制台会显示给我

无效的网址

 Alamofire.request("https://.../api/v1.8/set/order/?address=\(address)&email=\(email)&information=\(information)&name=\(name)&order=\(parameters)&password=\(password)&paymentType=\(paymentType)&phone=\(phone)&token=\(token)&userID=\(userID)&wihtRegistration=\(wihtRegistration)").validate(statusCode: 200..<300)
        .responseJSON { response in
        switch response.result
        {
        case .failure(let error):
            print(error)

        case .success(let value):
             print(value)

        print("Request: \(response.request)")

    }
}

如何在 Alamofire 中转换?

【问题讨论】:

  • “转换”是什么意思?我认为“invalidURL”错误很清楚,你应该检查“https://.../api/v1.8/set/order/?address=(address)&email=(email)&information=(information) &name=(name)&order=(parameters)&password=(password)&paymentType=(paymentType)&phone=(phone)&token=(token)&userID=(userID)&wihtRegistration=(wihtRegistration)"
  • "address=(address)":如果有空格,可能会失败。另外,为什么不使用那里的参数来为你做呢?
  • 没有空格,为了我需要请求 [{:}] 订购
  • 如果你这样做 let url = URL(string:yourString)url 不是 nil
  • 不,我通过Alamofire.request("https://..")

标签: swift request alamofire


【解决方案1】:

我创建了这个自定义方法。通过传递所需的参数来调用它:-

没有 JSON 编码

func requestWithoutJSONEncoding(_ method: HTTPMethod
    , _ URLString: String
    , parameters: [String : AnyObject]? = [:]
    , headers: [String : String]? = [:]
    , completion:@escaping (Any?) -> Void
    , failure: @escaping (Error?) -> Void) {

    Alamofire.request(URLString, method: method, parameters: parameters, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:
                completion(response.result.value!)
            case .failure(let error):
                failure(error)
            }
    }
}

使用 JSON 编码

func request(_ method: HTTPMethod
    , _ URLString: String
    , parameters: [String : AnyObject]? = [:]
    , headers: [String : String]? = [:]
    , completion:@escaping (Any?) -> Void
    , failure: @escaping (Error?) -> Void) {

    Alamofire.request(URLString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:
                completion(response.result.value!)
            case .failure(let error):
                failure(error)
            }
    }
}

【讨论】:

    【解决方案2】:

    试试这个

     let jsonObject = [["code": 404, "counts": 15]]
    
        let json = JSON(jsonObject)
    
        // Generate the string representation of the JSON value
        let jsonString = json.rawString(.utf8)!
        let params = ["name": name, "phone": phone, "address": address, "information": information, "email": email, "userID":userID, "wihtRegistration": wihtRegistration, "password": password, "token": token, "paymentType": paymentType, "order" : jsonString] as [String : Any]
    
        Alamofire.request("http:...", method: .get, parameters: params)
            .responseString { response in
                #if DEBUG
    (request!.url!.absoluteString)\n\(request!.httpBody.map { body in String(data: body, encoding: .utf8) ?? "" } ?? "")")
                switch response.result {
                case .success(let value):
                    print("Response with content \(value)")
                case .failure(let error):
                    print("Response with error: \(error as NSError): \(response.data ?? Data())")
                }
                #endif
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 2017-06-10
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多