How to cache response in AFHTTPRequestOperationMnager - ios

This is my code. I set cachePolicy = DontLoad, but it still get data from server not from local cache. Why?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
manager.responseSerializer = responseSerializer;
[manager GET:#"http://www.github.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#", [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];

Related

API with text/plain response failed with 'unacceptable content-type'

I'm trying to perform a GET request that responses with 'text/plain'
The request failed with error "unacceptable content-type: text/plain".
so I set the AcceptableContentType to "text/plain" and created a responseSerializerwithNSJSONReadingAllowFragments`.
But the request Also failed with error .
NSCocoaErrorDomain" - code: 3840" : "Invalid value around character 0."
NSString *requestUrl = [NSString
stringWithFormat:#"%#%#",kServerURL,myURL];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager.responseSerializer
setAcceptableContentTypes:[NSSet setWithObject:#"text/plain"]];
[manager GET:requestUrl
parameters:nil
progress:nil
success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
in this place
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager.responseSerializer
setAcceptableContentTypes:[NSSet setWithObject:#"text/plain"]];
use
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];

How to add raw body in AFHTTPSessionManager request?

I want To add Raw body in my AFHTTPSessionManager request
AFHTTPRequestOperationManager *manager;
manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:10.0];
[manager POST:strRequestUrl parameters:dictParams success:^(AFHTTPRequestOperation *operation, id responseObject)
{
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];

Post request in afnetworking 3.0?

I had an application in which I am using Afnetnetworking3.0 for web service calls like this.
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
NSURL * urlStr = [NSURL URLWithString:path];
NSDictionary *parameters = #{#"ID":#"fdfdfd",#"No":#"1414"};
NSLog(#"%#",parameters);
[manager POST:urlStr.absoluteString
parameters:parameters
progress:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
But it is adding some spaces in parameters dictionary and making the URL not found. Can anybody tell me where I am going wrong ?
Try like this :
NSString *Loginurl = [NSString stringWithFormat:#"http://Enter your url here"];
NSDictionary *params = #{
#"ID":#"fdfdfd",
#"No":#"1414"
};
NSLog(#"Sent parameter to server 1 : %#",params);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[policy setValidatesDomainName:NO];
[policy setAllowInvalidCertificates:YES];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript",#"text/html", nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json",#"text/html",nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json",#"text/plain",nil];
[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"Response from server 1 : %#", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
[self getdata:responseObject];
}
failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(#"Error: %#", error);
}];
}
-(void)getdata:(NSDictionary*)RegisterData
{
}
check the log Sent parameter to server 1... and check your url is correct or not?

AFNetworking response cache issue?

I am sending GET request to server to getting json response. When i first do it i get some response but then i make changes in the database but still i get the same josn which i was getting.But on browser the json is updated on app it is not updated.I have used below code to make it work but nothing works for me.
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:api_key forHTTPHeaderField:#"Authorization"];
manager.requestSerializer = requestSerializer;
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
manager.responseSerializer = responseSerializer;
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"json is %#",responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
[self activityIndicator:#"hide"];
NSLog(#"Error: %#", operation.responseString);
}];
I have tried reloadignoringcachedata but this does not work.

Pass Json string in AFNetworking 2.0

i want to pass json string in AFNetworking i am using the code below to parse the data to the server.
My JSON String :
{"Queries":[{"Field":"text_all","Value":"Class1","IsPhrase":false,"Near":0}],"Filter":[{"Key":"Test","Value":["Test","Test2"]}],"Paging":0,"Count":10}
Code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:#“http://www.test.com” parameters:main_dict success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"responseString: %#", json);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];

Resources