AFNetworking - How to make POST request by ID - ios

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.

Related

Remove escape from serialized NSSdictionary

I am pretty new to serializing with json, and I am facing a weird issue.
I am trying to send an NSURLRequest with a josn. The json is first stored into an NSSMutableDictionary and eventually is serialized. The serialized json object I get is escaped, meaning it has "\" in it all over the place.
The json is getting sent a server, but is getting denied. According to the admin the json is getting denied because its escaped. How can I removed all the back slashes from the serialized json before sending it.
HELP. I tried creating an NSString then converting to NSData then serialized and failed. I tried NSArray and failed. At least I think I did those correctly.
Did I make a mistake somewhere? is there a better way to do this?
Thanks,
Sam.
I came up with the same problem try AFNetworking
https://github.com/AFNetworking/AFNetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSlog(#"value->%#", [responseObject objectForKey#"json_key"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

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

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 read a simple string from a POST request in AFNetworking (No JSON)

I'm using AFNetworking to communicate with a server through POST that responds with a simple string containing the information I need. I'm using the following code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST: MY_URL
parameters: MY_PARAMETERS
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//do something
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//etc.
}];
However, it seems that AFNetworking expects every response to be in JSON format because I get this error when I execute my 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=0x1566eb00
{NSDebugDescription=JSON text did not start with array or object and
option to allow fragments not set.}
How can I tell AFNetworking that it's OK that the response is not a JSON object? I've seen something involving AFHTTPClient, but it doesn't seem to be part of AFNetworking anymore.
You can tell the AFHTTPRequestOperationManager or AFHTTPSessionManager how to handle the response, e.g. before calling POST, you can do the following:
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
Then in your success block, you can convert the NSData to a string:
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
Having said that, you might want to contemplate converting your web service to return JSON response, as it's far easier to parse that way (and differentiate between a valid response and some server error).
NSLog(#"Response: %#", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
you can get response description details like below
NSLog(#"JSON: %#", [responseObject description]);
Much better way would be subclassing AFHTTPResponseSerializer and overriding there
func responseObject(for response: URLResponse?, data: Data?, error:
NSErrorPointer) -> Any?
There you can parse response, cast to type you need and return.

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

Resources