Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking says "cannot parse response"

I am sending a request to server, which is handling the request and responsding. However on my app I am receiving:

Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x167668d0     
{NSErrorFailingURLStringKey=https://***, _kCFStreamErrorCodeKey=-1, 
NSErrorFailingURLKey=https://***/, NSLocalizedDescription=cannot
parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16731990 "cannot parse
response"

Accept field in http request is adequate.

I cannot even see what messege is arriving, because NSHTTPURLResponse object is null.

What can be an issue and in what way I can see what message is coming not using things like wireshark.

Thank You!

like image 236
galvanize Avatar asked Oct 28 '14 14:10

galvanize


4 Answers

I got the same problem, and the reason is that the API require GET http method, and I need pass a parameter, I send it by the following code:

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

When I execute the request with GET method, it will report the error.

My solution is when the API need GET method, the parameter should add on the URL, such as http://apiserver/api?paramName=paramValue, when the API method is POST, then use the code above, set the HTTPBody for the request object.

like image 159
liwp_Stephen Avatar answered Oct 16 '22 20:10

liwp_Stephen


If iOS cannot parse response, the problem must be in improper format http response from server, even if You are said, that everything is 100% correct, bacause other services communicate with it.

In my case an Android app communicated successfully, because network framework used there was not so restrictive and even if the content-length of http response does not fit the actual length of data, it could read this message, unlike ios.

Use tcpdump to check http communication!

like image 36
galvanize Avatar answered Oct 16 '22 19:10

galvanize


I got the same issue when I tried to call a web method using http GET method instead of http POST. The web service method was expecting POST request not GET. Hope this information will help some one.

like image 31
Nitheesh George Avatar answered Oct 16 '22 21:10

Nitheesh George


If you are using (iOS8 has a problem with this but works in iOS7)

NSData *dataToSend = [jsonData dataUsingEncoding:NSUTF16BigEndianStringEncoding] 

to send to the server, please make it as per below:

NSData *dataToSend = [jsonData dataUsingEncoding:NSUTF8StringEncoding]
like image 21
saurabh Avatar answered Oct 16 '22 21:10

saurabh