尝试使用GoogleShortener API获取URL的短版本。我正在使用AFNetworking 3.0,并收到以下错误:"AnyObject不是"NSProxy"的子类型
let manager = AFHTTPSessionManager()
manager.requestSerializer = AFJSONRequestSerializer()
let params = ["longUrl": "MYURL"]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=MYKEY", parameters: params, success: {(operation: NSURLSession!,responseObject: AnyObject!) in
println("JSON" + responseObject.description)
},
failure: { (operation: NSURLSession!,error: NSError!) in
println("Error while requesting shortened: " + error.localizedDescription)
})
它在"println("JSON"+responseObject.description)"行突出显示关于"描述"的乞求。
我在使用AFNetworking 3时遇到了同样的错误。文件显示有一些变化。但是,以下是我能够运行的代码。希望它能成功。
let manager = AFHTTPSessionManager()
manager.requestSerializer = AFJSONRequestSerializer()
let params = ["longUrl": "MYURL"]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=MYKEY", parameters: params, success: {(operation: NSURLSessionDataTask!,responseObject: AnyObject?) in
print("JSON" + (responseObject?.description)!)
},
failure: { (operation: NSURLSessionDataTask?,error: NSError) in
print("Error while requesting shortened: " + error.localizedDescription)
})
使用此代码请参阅此URL:http://cungcode.com/how-to-use-google-url-shortener-api-ios/
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer()
let params = ["longUrl": "YOUR_URL]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=MYKEY", parameters: params, success: {(operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
}, failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})