AFNetworking GET not working - (Invalid escape sequence around character 94.) - ios

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager GET:#"http://xml.memurlar.net/mobile/json/news/headlines.aspx" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am using latest AFNetworking. I want to get object from the link that I used above. But I get following error:
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid escape sequence around character 94.) UserInfo=0x1203e950 {NSDebugDescription=Invalid escape sequence around character 94.}
Any solution for that?

Yeah that API isn't returning a valid JSON structure. The problem is right here:
"KPSS\'de kim hangi oturuma girecek?"
That \ is not a valid escape character and needs to go. What are you using to serialize that JSON?

//do not use the default json serializer.
manager.responseSerializer=[AFHTTPResponseSerializer serializer];

Related

AFNetworking - How to make POST request by ID

I'm quite new to iPhone development, and I'm using AFNetworking as my services library.
The API i'm querying is a JSON one, and I need to make POST requests to fetch the data by ID where this is the detail view which fetched he News Image and Description and for the listing News i have used GET request and it's working fine in that GEt method i have NewsID. To do this, I tried with the following code :
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"News_Id": #"2",
#"News_Id": #"3",
#"News_Id": #"5"};
[manager POST:#"http://almithaq.mawaqaademo.com/AlMithaqService/API.svc/getNews_ByID" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
And added #"text/html", in acceptableContentTypes in AFJSONResponseSerializer now Getting the following error:
2014-07-15 12:22:18.030 News[2541:60b] 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=0x8cf0060 {NSDebugDescription=JSON text did not start with array or object and
option to allow fragments not set., NSUnderlyingError=0x8cefcb0 "Request failed: bad
request (400)"}
This seems like a problem with the server. You might want to check if you need any credentials to access the data.

Get non-json content from AFNetworking

I am trying to simply get the text displayed in the resulting page but it look like this code snipped is looking specifically for JSON only.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"userId": [loggedParent getObjectID]};
[manager POST:#"http://myurl" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
This is the error I get:
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=XXXXXXXXX {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
How do I modify my code to simply read and save the text that is displayed?
I'm assuming you're in AFNetworking 2.0, which I haven't actually used. But it looks like you should be able to modify the responseSerializer property in the AFHTTPRequestOperationManager to accept whatever content type your server is sending. AFHTTPResponseSerializer has an acceptableContentTypes field you can modify.
However, given the error you're seeing it seems like the server may be returning an "application/json" content-type but not actually delivering valid JSON content.

how to send EmailId or Device token as parameter using AFNetworking with AFHTTPRequestOperationManager

I need to send an email ID to a server to reset this email address using AFNetworking.
SSAFRequest *afm= [[SSAFRequest alloc]init];
NSDictionary *params = #{#"email": #"a#b.com"};
[afm AfRequest_POST_Params:urlWithQuerystring urlWithQuerystring:urlWithQuerystring params:params :^(id JSON, NSError *error) {
if(JSON!=Nil && ![JSON isEqual:[[NSNull alloc]init]]) {
}];
This is my AFNetworking code:
-(void)AfRequest_POST_Params:(NSString *)baseUrl urlWithQuerystring:(NSString *)urlstring params:(NSDictionary *)parameters:(void (^)(id result, NSError *error))block {
NSLog(#"AfRequest_POST_Params ----%# %#",baseUrl,urlstring);
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager]initWithBaseURL:[NSURL URLWithString:baseUrl]];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:baseUrl parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(#"****Success****");
block(responseObject,nil);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error.localizedDescription);
block(nil,error);
}
];
}
But I don't know what I am missing in my code. This is the error I am getting:
<0x16d95210 SSLoginViewController.m:(821)> Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x16ec5cd0 {NSDebugDescription=Invalid value around character 0.}
please suggest me on this, Thanks in advance.

AFNetworking can't get the responseObject

I want to get the responseObject. I tried ASI but people offer me to try AFNetworking.But I keep getting error. Where am I doing wrong?With SOAPUI I can get the results. Thank you.
The method in wcf:
ListHastaAra(int kat,int oda,string hastaAdi,int protokolNo,int yatanAyaktan);
the url that I use "http://... /rest /HastaAra"
IOS part:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"kat": #"0",#"oda": #"0", #"hastaAdi":hastaAdi, #"protokolNo":#"0",#"yatanAyaktan":#"1"};
[manager POST:HASTAARAIP parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
The error log is:
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=0x8c47a30 {NSDebugDescription=JSON text did not start with
array or object and option to allow fragments not set.}

Cocoa Error 3840 when POST Request with AFNetworking 2

I call this function and return Cocoa Error 3840 every time.
I try to debug and fix it and it error when request rather than when parse the result
I found this error in failure blocks when request.
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=0x109230960 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not et.}
2013-11-01 12:09:30.925 MagicBox[87431:70b] The operation couldn’t be completed. (Cocoa error 3840.)
That's my code
Thanks in Advance.
- (void)loginWithUserName:(NSString *)userName
Password:(NSString *)password
orFacebook:(NSString *)facebookID
withResponseBlock:(ResponseBlock)responseBlock {
if (!userName && !facebookID) {
NSError *error = [NSError errorWithDomain:#"Missing Parameters"
code:400
userInfo:#{ NSLocalizedDescriptionKey : #"Username or FacebookID is required"}];
responseBlock(error, nil);
}
NSDictionary *params;
if (facebookID) {
params = #{ #"fb_id": facebookID };
} else {
params = #{ #"username": userName,
#"password": password };
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *loginURL = [self requestWithPath:#"/api/login"];
[manager POST:loginURL
parameters:params
constructingBodyWithBlock:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseBlock) {
responseBlock(nil, responseObject);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (responseBlock) {
responseBlock(error, nil);
}
}];
}
I solved it by adding following line of code
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
Had been stuck with the same problem.
I solved it by this:
self.requestSerializer = [AFJSONRequestSerializer serializer];
i.e. double check whether the AFHTTPRequestOperationManager uses correct request serializer.
Hope this can help you!
I solved this by making sure the returned data is in the correct serialization with google spreadsheets XML response (instead of JSON)
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
I solved it by adding following line of code
manager.requestSerializer = [AFJSONRequestSerializer serializer];

Resources