1个回答
展开全部
首先要用到的东西就是把以前用的json数据在苹果中调用回来,昨天终于可以在Swift中使用 AFNetworking了,但是调用的时候发现,调用公开的返l回json的内容是没有问题的,自己写的服务,调用时却显示:Error: Request failed: unacceptable content-type: text/plain
这个应该是格式 的问题,于是baidu一下,找到了这篇文章:http://www.haogongju.net/art/2407859
使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法
添加一行
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
那我的是text/plain的错,应该把相应内容html改为plain就可以了,
但是还有一个问题,这个NSSet类型,在swift中怎么用呢,不知道
按下commond键,找一下帮助,前面的也看不太明白,看到这一段:
extension NSSet {
var allObjects: AnyObject[]! { get }
func anyObject() -> AnyObject!
func containsObject(anObject: AnyObject!) -> Bool
var description: String! { get }
func descriptionWithLocale(locale: AnyObject!) -> String!
func intersectsSet(otherSet: NSSet!) -> Bool
func isEqualToSet(otherSet: NSSet!) -> Bool
func isSubsetOfSet(otherSet: NSSet!) -> Bool
func makeObjectsPerformSelector(aSelector: Selector)
func makeObjectsPerformSelector(aSelector: Selector, withObject argument: AnyObject!)
func setByAddingObject(anObject: AnyObject!) -> NSSet!
func setByAddingObjectsFromSet(other: NSSet!) -> NSSet!
func setByAddingObjectsFromArray(other: AnyObject[]!) -> NSSet!
func enumerateObjectsUsingBlock(block: ((AnyObject!, CMutablePointer<ObjCBool>) -> Void)!)
func enumerateObjectsWithOptions(opts: NSEnumerationOptions, usingBlock block: ((AnyObject!, CMutablePointer<ObjCBool>) -> Void)!)
func objectsPassingTest(predicate: ((AnyObject!, CMutablePointer<ObjCBool>) -> Bool)!) -> NSSet!
func objectsWithOptions(opts: NSEnumerationOptions, passingTest predicate: ((AnyObject!, CMutablePointer<ObjCBool>) -> Bool)!) -> NSSet!
}
其中有一个 func setByAddingObject(anObject: AnyObject!) -> NSSet! 这个是返回一个NNSet的,应该可以用,
于是就有了以下代码:
@IBAction func showimage(sender : AnyObject) {
let manager = AFHTTPRequestOperationManager()
let url = "http://api.openweathermap.org/data/2.5/weather"
let ul="http://218.3.208.82:800/goldnurse/OFFER.GetOfferList.do"
println(url)
let params = ["lat": 39.26, "lon": 41.03, "cnt":0]
println(params)
var type="text/plain"
var sets=NSSet()
manager.responseSerializer.acceptableContentTypes = sets.setByAddingObject(type)
//[NSSet setWithObject:@"text/html"]
manager.GET(ul,
parameters: params,
success: { (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) in
println("JSON: " + responseObject.description!)
},
failure: { (operation: AFHTTPRequestOperation!,
error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
这个应该是格式 的问题,于是baidu一下,找到了这篇文章:http://www.haogongju.net/art/2407859
使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法
添加一行
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
那我的是text/plain的错,应该把相应内容html改为plain就可以了,
但是还有一个问题,这个NSSet类型,在swift中怎么用呢,不知道
按下commond键,找一下帮助,前面的也看不太明白,看到这一段:
extension NSSet {
var allObjects: AnyObject[]! { get }
func anyObject() -> AnyObject!
func containsObject(anObject: AnyObject!) -> Bool
var description: String! { get }
func descriptionWithLocale(locale: AnyObject!) -> String!
func intersectsSet(otherSet: NSSet!) -> Bool
func isEqualToSet(otherSet: NSSet!) -> Bool
func isSubsetOfSet(otherSet: NSSet!) -> Bool
func makeObjectsPerformSelector(aSelector: Selector)
func makeObjectsPerformSelector(aSelector: Selector, withObject argument: AnyObject!)
func setByAddingObject(anObject: AnyObject!) -> NSSet!
func setByAddingObjectsFromSet(other: NSSet!) -> NSSet!
func setByAddingObjectsFromArray(other: AnyObject[]!) -> NSSet!
func enumerateObjectsUsingBlock(block: ((AnyObject!, CMutablePointer<ObjCBool>) -> Void)!)
func enumerateObjectsWithOptions(opts: NSEnumerationOptions, usingBlock block: ((AnyObject!, CMutablePointer<ObjCBool>) -> Void)!)
func objectsPassingTest(predicate: ((AnyObject!, CMutablePointer<ObjCBool>) -> Bool)!) -> NSSet!
func objectsWithOptions(opts: NSEnumerationOptions, passingTest predicate: ((AnyObject!, CMutablePointer<ObjCBool>) -> Bool)!) -> NSSet!
}
其中有一个 func setByAddingObject(anObject: AnyObject!) -> NSSet! 这个是返回一个NNSet的,应该可以用,
于是就有了以下代码:
@IBAction func showimage(sender : AnyObject) {
let manager = AFHTTPRequestOperationManager()
let url = "http://api.openweathermap.org/data/2.5/weather"
let ul="http://218.3.208.82:800/goldnurse/OFFER.GetOfferList.do"
println(url)
let params = ["lat": 39.26, "lon": 41.03, "cnt":0]
println(params)
var type="text/plain"
var sets=NSSet()
manager.responseSerializer.acceptableContentTypes = sets.setByAddingObject(type)
//[NSSet setWithObject:@"text/html"]
manager.GET(ul,
parameters: params,
success: { (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) in
println("JSON: " + responseObject.description!)
},
failure: { (operation: AFHTTPRequestOperation!,
error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询