請(qǐng)求代碼如下:
// 參數(shù)
let param = ["access_token": (UserAccountViewModel.shareInstance.account?.access_token)!]
Alamofire.request("https://api.weibo.com/2/statuses/home_timeline.json", method: .get, parameters: param, encoding: JSONEncoding.default, headers: nil).responseJSON { (dataResponse) in
print("request---\(dataResponse.request!)")
print("response---\(dataResponse.response)")
print("data---\(dataResponse.data)")
print("resultValue---\(dataResponse.result.value)")
}
打印結(jié)果:
request---https://api.weibo.com/2/statuses/home_timeline.json
response---Optional( { URL: https://api.weibo.com/2/statuses/home_timeline.json } { status code: 403, headers {
"Content-Encoding" = gzip;
"Content-Type" = "application/json;charset=UTF-8";
Date = "Wed, 15 Nov 2017 07:38:42 GMT";
Server = "nginx/1.6.1";
Vary = "Accept-Encoding";
"api-server-ip" = "10.75.5.93";
} })
data---Optional(91 bytes)
resultValue---Optional({
error = "auth by Null spi!";
"error_code" = 21301;
request = "/2/statuses/home_timeline.json";
})
分析:
獲得數(shù)據(jù)失敗,打印結(jié)果得知request=https://api.weibo.com/2/statuses/home_timeline.json
沒(méi)有拼接參數(shù)令花,如果把encoding
值改為URLEncoding.default
是會(huì)有正確結(jié)果打印的
如果是get
請(qǐng)求 需要拼接字符串 那么就使用
URLEncoding.default
或者
URLEncoding(destination: .methodDependent)
說(shuō)明:
1锦担、JSONEncoding.default
是放在HttpBody
內(nèi)的俭识, 比如post
請(qǐng)求
2、URLEncoding.default
在GET
中是拼接地址的洞渔, 比如get
請(qǐng)求
3套媚、URLEncoding(destination: .methodDependent)
是自定義的URLEncoding
,methodDependent
的值如果是在GET
磁椒、HEAD
堤瘤、DELETE
中就是拼接地址的。其他方法方式是放在httpBody
內(nèi)的衷快。
4宙橱、URLEncoding(destination: .httpbody)
是放在httpbody
內(nèi)的