1
votes

I am using AFNetworking 2.0 in swift for calling The api is as follows http://api.openweathermap.org/data/2.5/weather?q=Delhi

When we hit this URL on browser, we will get a JSON. This is a GET request. On hitting this api on postman also produces the same result Now I made a helper class WebService.swift and in which I am calling a success and a error block

My WebService.swift class is as follows

import UIKit

class WebService: NSObject {

static let sharedInstance = WebService()


func weatherService(loginCode:String, requestData:NSMutableDictionary, successBlock: (response: AnyObject!,headers: NSDictionary, callBack:String ) -> Void, errorBlock: (error: NSError,headers:NSDictionary,responseObject:AnyObject!) -> Void){

    var serviceURL = "http://api.openweathermap.org/data/2.5/weather?q=Delhi"
    var manager = AFHTTPRequestOperationManager()
    manager.requestSerializer = AFJSONRequestSerializer()

    manager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/html","application/json")  as? Set<NSObject>


    manager.requestSerializer.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

    manager.GET(serviceURL, parameters: requestData, success: { (operation:AFHTTPRequestOperation, responseObject:AnyObject) -> Void in
        var headersCollection = operation.valueForKey("response")?.valueForKey("allHeaderFields") as! NSDictionary
        var headers = headersCollection.mutableCopy() as! NSMutableDictionary
        headers["statusCode"] = NSNumber(integer: operation.response!.statusCode)
           var _callBack = requestData["callBack"] as! String
        successBlock(response:responseObject,headers:headers,callBack: _callBack)


        }) { (operation:AFHTTPRequestOperation, error:NSError) -> Void in


            var headersCollection = operation.valueForKey("response")?.valueForKey("allHeaderFields") as! NSDictionary
            var headers = headersCollection.mutableCopy() as! NSMutableDictionary
            headers["statusCode"] = NSNumber(integer: operation.response!.statusCode)

            errorBlock(error: error, headers: headers, responseObject: operation.responseObject)
    }


}

} 

The method I am calling from my view controller in view did load

        let params = ["callBack": String(format: "%d", "weatherAPI")] as NSMutableDictionary

        WebService.sharedInstance.weatherService("URLHardcodedInWebServiceClass", requestData: params, successBlock: { (response, headers, callBack) -> Void in

            var dict = response as! NSDictionary


                println("Success with \(callBack)")

            }) { (error, headers, responseObject) -> Void in

                println("failed")

        }

Now the problem is that everytime I am going in the error block of webservice class where the contents of headers which is a NSMutableDictionary is

{
"Access-Control-Allow-Credentials" = true;
"Access-Control-Allow-Methods" = "GET, POST";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 31 Aug 2015 18:53:36 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
"X-Source" = back;
statusCode = 200;
}  

And when I see the NSError it shows

Printing description of error:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" 
(JSON text did not start with array or object and option to allow fragments not set.) 
UserInfo=0x7fdb6865d5a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

Status code I receive is 200 And when I do this po operation.responseString I get enter image description here

The json is there but with errors and I guess it is not formed correctly. I tried my other service with content-type as text/html and it worked fine as I have to change the line

manager.requestSerializer.setValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")

And URL for other service is http://bluesapphireindia.in/WebService.asmx/GetEmployessJSON

It is working fine in this piece of code

What can be the reason. Can anyone help me out in this. Thanks

1
Have u tried to set responseserializer to afhttpresponseserializer or afjsonresponseserializer ? - Ozgur Sahin

1 Answers

0
votes

I have done same thing in my current project in different way by using AFHTTPRequestOperation class from AFNetworking in Swift. I hope it may help you.

 func afnStartConnectionWithURL(urlEndPoint: String, enumName: enumAPIname, httpMethod: String, httpBody: AnyObject, headerValue: String){

    var _URL = NSURL(string: kBaseURL + urlEndPoint)
    var currentTime: NSTimeInterval = 10

    var request = NSMutableURLRequest(URL: _URL!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: currentTime)
    request.HTTPMethod = httpMethod
    var err: NSError?
    if(httpMethod == kPOST){

        let jsonData:NSData = NSJSONSerialization.dataWithJSONObject(httpBody, options: .PrettyPrinted, error: nil)!
        request.HTTPBody = jsonData
    }
    request.addValue(headerValue, forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    var op = AFHTTPRequestOperation(request: request)
    op.responseSerializer = AFJSONResponseSerializer()
    op.setCompletionBlockWithSuccess({ (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in

        operation.cancel()
        var dicConnectionResponse:NSDictionary = responseObject as! NSDictionary
        self.afnConnection_Result(dicConnectionResponse, enumName: enumName, afhttpRequestOperation: operation)

        },

        failure: { (operation: AFHTTPRequestOperation!, error:NSError!) -> Void in

            operation.cancel()
            self.afnConnection_Error(operation, enumName: enumName, error: error)
    })

    op.start()
}

here the afnConnection_Result and afnConnection_Error are call back delegate methods.