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)
{
}];
Related
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];
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) {
}];
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.
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) {
}];
I want to use AFNetworking class to my application to communicating with server.
I am new to this library . Here is my code:
NSDictionary *user=[[NSDictionary alloc]initWithObjectsAndKeys:#"hiteshp",#"userName",#"12345^",#"password", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
manager.requestSerializer=requestSerializer; //[AFJSONRequestSerializer serializer];
[manager POST:#"myURL" parameters:user success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %# %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I want to add header to this request . I tried but it is not working . If I wrote commented line then request is done but I also want to add one more field "Authorization" to requset.
Andhow to print request header?
I know it's too late but here's a code that makes what you want
NSString *finalyToken = [[NSString alloc]initWithFormat:#"Bearer %#",user.token];
AFHTTPRequestOperationManager *manager =
[[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"https://server1.appscserver.de/api"]];
[manager.requestSerializer setValue:finalyToken forHTTPHeaderField:#"Authorization"];
Hope it helps someone
Use this in your code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"text/html", #"application/json", nil];
//Authorization
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:userName password:password];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
in php file add:
header('Content-type: application/json');
in xcode:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
example xcode code:
NSDictionary *parameters = #{#"key1": #"value1",
#"key2": #"value2"};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:[NSString stringWithFormat:#"http://%#", Url] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];