24
votes

I am using AFJSONRequestOperation to request a server and parse the returned JSON response, but while parsing, I got this error:

NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";

I checked the API and it's returning JSON data:

header('Content-type: text/json');
$arr[] = array("Message" => "update succeeded");
echo '{"Result":'.json_encode($arr).'}';

Any idea how to fix that?

EDIT

I tried to call the API from browser and include the request in the url, and so I got a valid JSON response:

{"Result":[{"Message":"update succeeded"}]}
7
Could you post the JSON string that is received by AFJSONRequestoperation. - Mike D
Hi, the JSON I got in the failure block is (null) - Malloc
can you run the api in browser and include the response to the question? - Lithu T.V
Yeah I did already, please have a look on my edit :) - Malloc
I don't know PHP but shouldn't the entire thing you echo be wrapped in a JSON_encode? - Keith Smiley

7 Answers

1
votes

First thing, json_encode the entire object rather than breaking into it pieces.

Secondly, unless $arr contains multiple elements (not clear from example above), it should be initalized as thus:

$arr = array("Message" => "update succeeded");

I'm still not certain what else could be the issue here. You should echo out what your app is receiving and that should indicate the issue.

1
votes

Please use acceptable content type. in your webservice that should be only plain text.

here is my swift code and fixed:

    let manager = AFHTTPRequestOperationManager()

    manager.requestSerializer=AFJSONRequestSerializer()
    manager.responseSerializer = AFHTTPResponseSerializer();

    manager.GET(

        baseURL + (webServiceType as String) + secureParam,
        parameters:[:],

        success:
        { (operation: AFHTTPRequestOperation!,
            responseObject: AnyObject!) in
            completion(obj: responseObject)
        },
        failure:
        { (operation: AFHTTPRequestOperation!,
            error: NSError!) in
            completion(obj: nil)
    })
1
votes

Check you have added /api/ before your base url of API like

http:// someurl / yourBasrUrl /api/apiName

1
votes

To make a valid json response, your code should look something like this:

$response = array(
    "Result" => array(
        "Message" => "update succeeded"
    )
)

echo json_encode($response);
1
votes

If you need read fragment json you can use option .allowFragments like this:

JSONSerialization.jsonObject(with: someData, options: .allowFragments)
1
votes

Please try this:

let manager = AFHTTPSessionManager(baseURL: NSURL("Base URL"))
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer = AFJSONResponseSerializer()

 let params = [
    "foo": "bar"
]

manager.POST("API url", parameters: params, 

success: {
    (task: NSURLSessionDataTask!, responseObject: AnyObject!) in
        print("success") 
}, 

failure: {
    (task: NSURLSessionDataTask!, error: NSError!) in
        print("error")
})
0
votes

First of all, your API is returning improper Content-Type. Proper content type for JSON data is application/json. This may be conflicting while using third-party libraries.

Secondary, you should not produce json string "by hand". Altogether API should be modified to face:

header('Content-type: application/json');
$arr[] = array("Message" => "update succeeded");
echo json_encode(array('Result' => $arr));

Last bat not least. There is one more thing in charge possible: you might have BOM characters at very beginning of your api PHP script. Those are whitespace, so you may not see them in browser. Please, ensure that your PHP files are encoded without BOM.