import UIKit
import AFNetworking
import SVProgressHUD
enum RequestType :Int{
case GET = 0
case POST = 1
}
class XBNetworkTool: AFHTTPSessionManager {
static let sharedNetworkTool = XBNetworkTool()
//把get 和post方法写在一个方法中
func sendRequest(URLString : String,type : RequestType,params : AnyObject?,success : (JSON : AnyObject?)->(),failure : (error : NSError)->())
{
SVProgressHUD.show()
if(type == RequestType.GET)
{
XBNetworkTool.sharedNetworkTool.GET(URLString, parameters: params, progress: nil, success: { (_, JSON) -> Void in
success(JSON: JSON)
SVProgressHUD.dismiss()
}, failure: { (_, error) -> Void in
SVProgressHUD.showErrorWithStatus(error.description)
failure(error: error)
})
}
else if(type == RequestType.POST)
{
XBNetworkTool.sharedNetworkTool.POST(URLString, parameters: params, progress: nil, success: { (_, JSON) -> Void in
//成功回调方法的实现
success(JSON: JSON)
SVProgressHUD.dismiss()
}, failure: { (_, error) -> Void in
SVProgressHUD.showErrorWithStatus(error.description)
//失败了回调数据
failure(error: error)
})
}
}
}