最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】iOS中调用Alamofire出错:nw_endpoint_flow_service_writes ready socket-flow satisfiedWrite request has frame count 0 byte count

iOS crifan 775浏览 0评论

iOS中,swift代码,调用http的库Alamofire时,代码:

    func exportReportSummary(sender:UIButton){
        gLog.debug(“sender=\(sender)”)
        
        self.pleaseWait()
        
        
        var parameters = [String : AnyObject]()
        parameters = [
                “accessTokenId”: gCurUserItem.accessToken as AnyObject
        ]
        getUrlRespJson_async(
            httpMethod: .get,
            url:  ServerApi.getExportShopVisitReportUrl(visitId: self.id),
            parameters: parameters,
            respJsonHandle: { [weak self] (response) in
                self?.clearAllNotice()
                if response.isSuccess {
                    gLog.debug(response.successValue)
                } else if response.isFailure {
                    self?.noticeInfo(response.failedMessage)
                }
        })
    }

现象:

一直loading,网络请求无返回:

2017-09-18 15:58:23.142 [Info] [main] [VincentHttp.swift:61] getRequestRespJson(httpRequest:mergedAllPara:requsetType:respJsonHandler:) > httpRequest=GET http://123.206.101.36:9090/v1/skr/task/resource/visit/export/file/plan/visit_13429_20170906144751, mergedAllPara=[“parameters”: {
    accessTokenId = gtlrl8vsk59agk0a0tlcrffa59;
}, “httpMethod”: GET, “url”: http://123.206.101.36:9090/v1/skr/task/resource/visit/export/file/plan/visit_13429_20170906144751], respJsonHandler=(Function)
2017-09-18 15:58:23.142911+0800 SRTDev[17438:12772137] [] nw_endpoint_flow_service_writes [1.1 123.206.101.36:9090 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
2017-09-18 15:58:53.155762+0800 SRTDev[17438:12772040] [] nw_endpoint_flow_service_writes [1.1 123.206.101.36:9090 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
2017-09-18 15:58:53.160449+0800 SRTDev[17438:12774120] [] tcp_connection_write_eof_block_invoke Write close callback received error: [89] Operation canceled
2017-09-18 15:58:53.190530+0800 SRTDev[17438:12774120] [] nw_endpoint_flow_service_writes [11.1 123.206.101.36:9090 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count

搜:

nw_endpoint_flow_service_writes ready socket-flow (satisfied)] Write request

Sending GET with JSON in body gets Timeout error · Issue #1819 · Alamofire/Alamofire

我此处,貌似也是对于:

get来说,用的是:

func getUrlRespJson_async(httpMethod:Alamofire.HTTPMethod,url:String,parameters: [String : AnyObject]? = nil,headers:[String:String]? = nil, requsetType: SRTRequsetType = .json,respJsonHandle:@escaping (_ httpResult: SRTHttpResult)-> Void) {
    mergedExtraPara[“parameters”] = parameters as AnyObject?
    let curHttpReq = Alamofire.request(url, method: httpMethod, parameters: parameters, encoding: JSONEncoding.default, headers: curHeaders)

对于get来说,query string,感觉应该是加到url中,应该用的是:

URLEncoding

所以此处解决办法有两种:

1是调用此处的getUrlRespJson_async之前,就把参数放到url中 ?key=value

2.是放到此处的parameters, 然后代码中通过是get就用URLEncoding,如果是POST或PUT就用JSONEncoding

现在去用第二种

func getUrlRespJson_async(httpMethod:Alamofire.HTTPMethod,
                          url:String,parameters: [String : AnyObject]? = nil,
                          headers:[String:String]? = nil,
                          requsetType: SRTRequsetType = .json,
                          respJsonHandle:@escaping (_ httpResult: SRTHttpResult)-> Void) {
    var paramEncoding:ParameterEncoding = JSONEncoding.default
    if (httpMethod == .get) {
        paramEncoding = URLEncoding.default
    }
    
//    let curHttpReq = Alamofire.request(url, method: httpMethod, parameters: parameters, encoding: JSONEncoding.default, headers: curHeaders)
    let curHttpReq = Alamofire.request(url, method: httpMethod, parameters: parameters, encoding: paramEncoding, headers: curHeaders)

然后就可以正常传递参数,返回正确结果了:

对应url是我们希望的:

http://x.x.x.x:9090/v1/skr/task/resource/visit/export/file/plan/visit_13429_20170906144751?accessTokenId=ng6j540efljmtct8o5dvo4uqks

【总结】

此处,之所以调用Alamofire出现nw_endpoint_flow_service_writes的错误:,是因为:

在get时,设置了parameters,但是编码时用的是(POST/PUT等才该用的)JSONEncoding

解决办法:

当get时,把JSONEncoding换成URLEncoding,即可。

相关代码:

    var paramEncoding:ParameterEncoding = JSONEncoding.default
    if (httpMethod == .get) {
        paramEncoding = URLEncoding.default
    }
    
//    let curHttpReq = Alamofire.request(url, method: httpMethod, parameters: parameters, encoding: JSONEncoding.default, headers: curHeaders)
    let curHttpReq = Alamofire.request(url, method: httpMethod, parameters: parameters, encoding: paramEncoding, headers: curHeaders)

转载请注明:在路上 » 【已解决】iOS中调用Alamofire出错:nw_endpoint_flow_service_writes ready socket-flow satisfiedWrite request has frame count 0 byte count

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
67 queries in 0.241 seconds, using 18.84MB memory