iOS AFNetworking post troubles - ios

I am currently trying to post to a php web service which will query a mysql database and [should] return with a string value. I have looked into AFNetworking but do not understand how to use the value which is returned from the query. for example:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://samwize.com/api/poo/"
parameters:#{#"color": #"green"}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am guessing I manipulate responseObject to get my expected string? Any help would be appreciated

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"http://samwize.com/api/poo/"
parameters:#{#"color": #"green"}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
NSLog(#"Error serializing %#", error);
}
NSLog(#"JSON: %#", JSON);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
This will convert your JSON response to NSDictionary

Related

Getting Error While Post Data Using AFnetworking

This is my Code:
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *dict = #{#"body":json};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:#"http://abc/" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSString *myString = [[NSString alloc] initWithData:operation.request.HTTPBody encoding:NSUTF8StringEncoding];
NSLog(#"Error: %#", myString);
}];
But i am Getting Error Like This
body=%7B%22pincode%22%3A%22gfgfgf%22%2C%22mobile%22%3A%221111%22%2C%22password%22%3A%22rwerew%22%2C%22profile_pic%22%3A%22%22%2C%22social_accesstoken%22%3A%22%22%2C%22sponsor_choice%22%3A3%2C%22sponsor_id%22%3A%22%22%2C%22social_provider%22%3A%22Normal%22%2C%22email%22%3A%22ghffh%22%2C%22gender%22%3A1%2C%22name%22%3A%22narendra%22%2C%22nickname%22%3A1233%2C%22country%22%3A%22Japan%20%28JPY%29%22%2C%22fromLogin%22%3A%22%22%2C%22methodName%22%3A%22signup%22%2C%22birth_date%22%3A%2208-06-01%22%7D
Can any one suggest me on this kind...
There is no need to convert error to encoded string....
First of all clear that.. which type of parameter you needed..
and then try this code for simple posting data:
(Make related changes according to your parameters needed)
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *param = # {#"body":json};
[manager POST:URL_HERE parameters:param
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

AFNetworking not receiving response data

I am using AFNetworking to GET request by posting json object. But i am receiving empty value. I am using the following code.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = # {#"email" :user_name, #"password" :pass_word };
[manager GET:HOST_URL parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
NSDictionary *weather = (NSDictionary *)responseObject;
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"opstr%#",operation.responseString);
}];
What method i should use. If i use POST, i am getting error saying that "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.)
When i use GET i am getting response as empty. I am not sure where i am wrong.
Thanks in advance.
It means that your request response is not a valid json. You can correct the web service to return proper json. Or remove the following line
manager.requestSerializer = [AFJSONRequestSerializer serializer];
because it is converting response to json.
To allow fragments, change the code to following
[manager GET:HOST_URL parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
id json = [NSJSONSerialization JSONObjectWithData:responseObject
options:NSJSONReadingAllowFragments
error:&deserializingError];
NSLog(#"JSON: %#", responseObject);
NSDictionary *weather = (NSDictionary *)responseObject;
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"opstr%#",operation.responseString);
}];

How to make a rest API Call post with multiple parameter

I am trying to have an API Call post. I can use this post command however my issue is that I have multiple parameters such as, Name, phone number, email, and multiple other private information. I have tried this method in order to get it to work but have not been successful. Thank you in advance for any help!
GET Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
or
POST URL-Form-Encoded Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar"};
[manager POST:#"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar", #"key_param2":#"obj_param2", #"key_param3":#"obj_param3",#"key_paramN":#"obj_paramN"};
[manager POST:#"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
To add more parameters, just include them in your parameters dictionary like so:
NSDictionary *parameters = #{#"foo": #"bar", #"otherFoo":#"otherBar"};
Then pass 'parameters' to your post method exactly as you are currently doing.

How to save a responseObject in a Get request AFNetworking to nsarray?

I've already killed a couple terrible hours to find out how to save a responseObject in a Get request AFNetworking to nsarray. Can't figure out how to save it so I could use it in another part of my code.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
__block NSArray* responseObjectArray = nil;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
responseObjectArray = (NSArray*)responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

AFNetworking GET request with query is not working

this is always error with -1004 ( could not connect to server )
but the request in browser is working fine.
what's wrong with my code?
(url : "http://localhost:3000/v1/voice/version?appname=wevoice")
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:#"http://localhost:3000/v1/voice/version"
parameters:#{ #"appname": #"wevoice" }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
You have to set the property baseURL on your request manager. Here is an example:
self.requestManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:http://localhost:3000];
Later, in your concrete GET message to the manager, you'll only pass the relative URL path:
[self.requestManager GET:#"/v1/voice/version"
parameters:#{ #"appname": #"wevoice" }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
And keep in mind, that, if you want to send your paramters as JSON, you'll need a request serializer:
self.requestManager.requestSerializer = [AFJSONRequestSerializer serializer];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:#"wevoice" forKey:#"appname"];
[manager GET:#"http://localhost:3000/v1/voice/version"
parameters:dic
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
You can try this.May be the way you add Parameter is wrong.
Try this method. Hope it helps.
+ (void)getRequestWithURLStringWithSuccess:(void(^)(id object))successBlock andFail:(void(^)(NSString *errorMessage))failBlock {
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:#"http://localhost:3000/v1/voice/version"]];
NSString *urlString = #"/version";
[manager GET:urlString parameters:#{ #"appname": #"wevoice" } success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}

Resources