2

这是我的端点的外观:

extension MyEndpoint: TargetType {
    var baseURL: NSURL { return NSURL(string: "http://10.0.1.13:5000")! }
    var path: String {
        switch self {
        case .SearchForNodes(_, _, _):
            return "/api/search/node/"
        case .CreateHistoricalEvent(_):
            return "/api/node/historicalevent/"
        }
    }
    var method: Moya.Method {
        switch self {
        case .SearchForNodes(_, _, _):
            return .GET
        case .CreateHistoricalEvent(_):
            return .POST
        }
    }
    var parameters: [String: AnyObject]? {
        switch self {
        case .SearchForNodes(let startsWith, let page, let pageSize):
            return ["startsWith": startsWith, "page": page, "pageSize": pageSize]
        case .CreateHistoricalEvent(let title):
            return ["Title": title]
        }
    }

    var parameterEncoding: Moya.ParameterEncoding {
        switch self {
        case .CreateHistoricalEvent:
            return ParameterEncoding.Json
        default:
            return ParameterEncoding.URL
        }
    }
}

现在我想让我的 CreateHistoricalEvent 方法将其参数作为 JSON 发布到请求正文中。我错过了什么?

4

2 回答 2

3

在 Swift3 中

改变

    var parameterEncoding: Moya.ParameterEncoding {
        switch self {
        case .CreateHistoricalEvent:
            return ParameterEncoding.Json
        default:
            return ParameterEncoding.URL
        }
    }

   var parameterEncoding: Moya.ParameterEncoding {
        return JSONEncoding.default
    }

如果你想用 JsonArray 发送数据,试试:

import Alamofire
struct JsonArrayEncoding: Moya.ParameterEncoding {

    public static var `default`: JsonArrayEncoding { return JsonArrayEncoding() }


    /// Creates a URL request by encoding parameters and applying them onto an existing request.
    ///
    /// - parameter urlRequest: The request to have parameters applied.
    /// - parameter parameters: The parameters to apply.
    ///
    /// - throws: An `AFError.parameterEncodingFailed` error if encoding fails.
    ///
    /// - returns: The encoded request.
    public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var req = try urlRequest.asURLRequest()
        let json = try JSONSerialization.data(withJSONObject: parameters!["jsonArray"]!, options: JSONSerialization.WritingOptions.prettyPrinted)
        req.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        req.httpBody = json
        return req
    }

}

    var parameterEncoding: Moya.ParameterEncoding {
        switch self {
        case .CreateHistoricalEvent:
            return JsonArrayEncoding.default
        default:
            return JSONEncoding.default
        }
    }
于 2017-02-20T06:46:13.930 回答
0
extension MoyaProvider {
    public class func JSONEndpointMapping(target: Target) -> Endpoint<Target> {

        let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString

        return Endpoint(
            URL: url,
            sampleResponseClosure: {
                .NetworkResponse(200, target.sampleData)
            },
            method: target.method,
            parameters: target.parameters,
            parameterEncoding: Moya.ParameterEncoding.JSON
        )
    }
}

let DemoAPI = MoyaProvider<DemoTarger>(endpointClosure: MoyaProvider.JSONEndpointMapping)
于 2016-07-22T04:15:59.753 回答