1

尝试使用 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)
    })

它在 'description' 的乞求行'println("JSON" + responseObject.description)' 上突出显示。

4

2 回答 2

0

使用此代码请参阅此 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)
    })
于 2016-06-01T18:26:17.457 回答
0

我在使用 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)
})
于 2016-06-01T18:18:23.990 回答