4

This is my url String with paramaters. http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1 through which I am getting my JSON data. I have AFWrapper.swift file in which I have defined function for GETrequest.

import UIKit
import Alamofire
import SwiftyJSON

class AFWrapper: NSObject {

    class func requestGETURL(strURL: String, params : [String : AnyObject]?, success:(JSON) -> Void, failure:(NSError) -> Void) {
        Alamofire.request(.GET, strURL, parameters: params, encoding: ParameterEncoding.JSON).responseJSON { (responseObject) -> Void in

            print(responseObject)

            if responseObject.result.isSuccess {
                let resJson = JSON(responseObject.result.value!)
                success(resJson)
            }
            if responseObject.result.isFailure {
                let error : NSError = responseObject.result.error!
                failure(error)
            }


           }
        }
}

Now I am calling this function in my ViewController.swift file.

let strURL = "http://api.room2shop.com/api/product/GetProducts"
    let param = ["categoryId": "22", "filter": "2", "pageNumber": "1"]
    AFWrapper.requestGETURL(strURL, params: param, success: {
        (JSONResponse) -> Void in

        if let resData = JSONResponse["ProductList"].arrayObject {
            for item in resData {
                self.TableData.append(datastruct(add: item as! NSDictionary))
            }

        do
        {
            try self.read()
        }
        catch
        {
        }
        self.do_table_refresh()
    }

}) {
    (error) -> Void in
    print(error)
}

but it is not giving me any response and giving me this error.

FAILURE: Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSErrorFailingURLStringKey=http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://api.room2shop.com/api/product/GetProducts, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x78ecf180 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1}}} Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSErrorFailingURLStringKey=http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://api.room2shop.com/api/product/GetProducts, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x78ecf180 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1}}}

Can anyone tell me what am I doing wrong? I have seached this link but not getting what is wrong. URL Encode Alamofire GET params with SwiftyJSON

Community
  • 1
  • 1
Riddhi Shah
  • 733
  • 4
  • 11
  • 25

3 Answers3

15

i think you should remove the parameter of "encoding: ParameterEncoding.JSON",like this:

Alamofire.request(.GET, strURL, parameters: params).responseJSON { (responseObject) -> Void in

        print(responseObject)

        if responseObject.result.isSuccess {
            let resJson = JSON(responseObject.result.value!)
            success(resJson)
        }
        if responseObject.result.isFailure {
            let error : NSError = responseObject.result.error!
            failure(error)
        }


}
michael wang
  • 553
  • 3
  • 11
2

Use this code. It is retrieving response correctly parsed in JSON.

Using Alamofire v3.0+

Alamofire.request(.GET, "http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1")
        .responseJSON { response in
            debugPrint(response)

            switch response.result {
            case .Success(let JSON):
                print(JSON)
            case .Failure(let error):
                print(error)
            }
    }

EDIT: For accepting Parameters with GET Type Service:

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
     .responseData { response in
         print(response.request)
         print(response.response)
         print(response.result)
      }

In this case try to not manipulate your URL String and send all parameters in terms of Dictionary like this.

n.by.n
  • 2,458
  • 22
  • 31
  • Yah I know direct URL is giving response correctly. But I want to pass parameters @ n.by.n – Riddhi Shah Apr 15 '16 at 08:55
  • There are two ways your Web Service takes parameters either by String Manipulation (i.e giving parameters inside URL String) or making a service that accepts parameters in JSON format, but in the latter case your URL should be clean without any parameters inside it. (this is good practice). For taking parameters as JSON inside a GET service I am editing my answer. Check! – n.by.n Apr 15 '16 at 09:02
1

Your requestGETURL should look like that

    func requestGETURL(strURL: String, params: [String:String]?, success: (AnyObject?) -> Void, failure: (NSError) -> Void) {
    Alamofire.request(.GET, strURL, parameters: params).responseJSON {
        (responseObject) -> Void in

        print(responseObject)

        if responseObject.result.isSuccess {
            let resJson = JSON(responseObject.result.value!)
            success(resJson)
        }
        if responseObject.result.isFailure {
            let error: NSError = responseObject.result.error!
            failure(error)
        }


    }
}

Your problem was in params it should be [String:String] dictionary. Also you don't have to declare encoding encoding:ParameterEncoding.JSON.

Hope it help you

kamwysoc
  • 6,709
  • 2
  • 34
  • 48