Swift 阿拉莫菲尔斯威夫特转换

Swift 阿拉莫菲尔斯威夫特转换,swift,request,alamofire,Swift,Request,Alamofire,嗨,我想向API发出请求,但当发送时,控制台会显示给我 无效URL Alamofire.request(“https://.../api/v1.8/set/order/?address=\(地址)&email=\(电子邮件)&information=\(信息)&name=\(名称)&order=\(参数)&password=\(密码)&paymentType=\(支付类型)&phone=\(电话)&token=\(令牌)&userID=\(userID)&wihtRegistration=\(w

嗨,我想向API发出请求,但当发送时,控制台会显示给我

无效URL


Alamofire.request(“https://.../api/v1.8/set/order/?address=\(地址)&email=\(电子邮件)&information=\(信息)&name=\(名称)&order=\(参数)&password=\(密码)&paymentType=\(支付类型)&phone=\(电话)&token=\(令牌)&userID=\(userID)&wihtRegistration=\(wihtRegistration)”。验证(状态代码:200..我创建了这个自定义方法。通过传递所需参数调用它:-

没有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)
            }
    }
}
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)
            }
    }
}
使用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)
            }
    }
}
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)
            }
    }
}
试试这个

 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
    }

“转换”是什么意思?我认为“invalidURL”错误很明显,您应该检查“https://.../api/v1.8/set/order/?address=(地址)&email=(电子邮件)&information=(信息)&name=(名称)&order=(参数)&password=(密码)&paymentType=(paymentType)&phone=(电话)&token=(令牌)&userID=(userID)&wihtRegistration=(wihtRegistration)”“address=(address)”:如果它有空格,它可能会失败。另外,为什么不使用那里的参数为您执行此操作?如果没有空格,我需要为order请求[{:}],如果您执行
let url=url(string:yourString)
is
url
not
nil
?不,我传递
Alamofire.request(”https://..“”