AFNetworking can't get the responseObject - ios

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.}

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.

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

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];

How to make a POST request in iOS?

I am trying to figure out how to make use of AFNetworking 2.0. I am trying to POST a login with a username and password.
This is my current attempt but something is not working as it does not get sent.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"loginName": #"password"};
[manager POST:#"http://xxxx.com/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSLog(#"Data saved");
MainViewController *mainView = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
mainView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController: mainView animated:YES completion:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSString *error_msg = (NSString *) error;
[self alertStatus:error_msg :#"Sign in Failed!" :0];
}];
}
} #catch (NSException * e) {
NSLog(#"Exception: %#", e);
[self alertStatus:#"Sign in Failed." :#"Error!" :0];
}
However nothing is happening, just keep getting this printout:
2014-04-16 20:14:09.903 Slidedrawer[9279: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=0x8e78bd0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
The response will be in JSON. Anyone know what I need to edit in the code or how to fix it?
JSON: {
"_id" = 533cb1c453769a02008c2d55;
name = dddd;
picture = "/img/users/default.jpg";
}
How do I access the above returned JSON, trying to pick apart each returned property to a string or integer etc...
Try this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"userName": username, #"password": password};
manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format
[manager POST:#"http://asd.com/login.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"%#", error);
}];
For parsing the response use this the responseObject as NSDictionary:
NSString *userName = [responseObject objectForKey:#"userName"];
NSString *password = [responseObject objectForKey:#"password"];
Hope this helps.. :)
You may find Send Nested JSON using AFNetworking for the answer.
The error Error Domain=NSCocoaErrorDomain Code=3840 is received invalid JSON object from the server. It is probably caused by the server's improper handling your missing parameters . Your parameters dictionary has only one key object loginName.

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.

Resources