Using AFHTTPRequestOperation to construct URL (curly braces included) - ios

I need to construct URL like this using AFNetworking the problem for me it's { and } how to pass throught parameter
/api/sth.json?filter[condition]={"53891":[123],"53892":[123,124]}
So my code looks like this (i made it simpler):
[self GET:myUrl parameters:#{
#"filter" : #{
#"condition" : #{
#"53891" : #[#(123)],
#"53892" : #[#(123),#(124)]}
},
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation,error);
}];
But it's produce not expected output:
/api/sth.json?filter[condition][53891][]=123&filter[condition][53892][]=123&filter[condition][53892][]=124
There is a way to do this in parameters in AFHTTPRequestOperation or manually i have to put it into string?
EDIT:
My current solution is like that:
+(NSString*)convertFromDictionary:(NSDictionary*)dic {
NSMutableString *outputStr = [NSMutableString new];
[outputStr appendString:#"{"];
NSArray *allKeys = [[dic allKeys] sortedArrayUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:nil ascending:NO]]];
for(NSString *key in allKeys) {
NSArray *objects = dic[key];
[outputStr appendString:[NSString stringWithFormat:#"\"%#\":[",key]];
for(NSNumber *nb in objects) {
[outputStr appendString:[NSString stringWithFormat:#"%li",[nb longValue]]];
if(![nb isEqual:[objects lastObject]]) {
[outputStr appendString:#","];
} else {
[outputStr appendString:#"]"];
}
}
if(![key isEqual:[allKeys lastObject]]) {
[outputStr appendString:#","];
}
}
[outputStr appendString:#"}"];
return outputStr;
}
Where input dictionary is:
#{#"53892" : #[#(123),#(124)]}
But it's nothing more than string compare. There is no more clever way to achieve it with AFNetworking directly since it's fairly standard URLs parameters?

You want 2 different ways of parsing a dictionary, and this cannot be done automatically. As Mehul said before, try serializing your parameter "condition" (converting its value to string) before creating the "parameters" dictionary:
NSError *error;
NSDictionary *dict = #{#"53891" : #[#(123)], #"53892" : #[#(123),#(124)]};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
NSString *params = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[self GET:myUrl parameters:#{
#"filter" : #{
#"condition" : params
},
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(operation,responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(operation,error);
}];

You need to do this:
create dictionary
create json string from dictionary(first convert dictionary to NSData using NSJsonSerialization and then convert thata NSData object to NSString)
Now append this string in your URL in format which you want to attach to
create url from new string and pass it in get/post method with paramters dictionary as nil

Wrapping a numbers with parenthesis is for expressions :
#(6 + x * 2012)
Read excellent explanation post https://stackoverflow.com/a/9349981/3096087
Maybe wrapping in parenthesis somehow messes up with AFHTTPRequestOperation.
NSNumber are directly declared the following way :
NSNumber *number = #123;
So try with an input dictionary the following way :
#{#"53892" : #[#123,#124]}

Related

Objective C Accessing Elements

I'm attempting to obtain elements extracted from a dictionary and convert them to doubles. The data is being pulled from JSON and seems to be extracted into a type of array (not sure which type). Is there a way to obtain the numbers listed below individually out of the array? Please let me know if you need more information.
NSDictionary *parameters = #{#"username":savedUser,#"password":savedPass};
NSURL *URL = [NSURL URLWithString:#"testwebsite"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:URL.absoluteString parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
NSError *error = nil;
JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if (error) {
NSLog(#"Error serializing %#", error);
}
NSLog(#"%#",JSON);
NSString *price = [NSString stringWithFormat:#"%#",[JSON valueForKey:#"UnitPrice"]];
price= [price stringByReplacingOccurrencesOfString:#"\"" withString:#""];
NSLog(#"Price: %#",price);
[transactionTotals addObject:price];
[self createGraph:100];
}
failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(#"Error1: %#", [error debugDescription]);
NSLog(#"Error2: %#", [error localizedDescription]);
}];
}
#catch (NSException *exception)
{
NSLog(#"%#",exception);
}
Log (UnitPrice values I need individually extracted):
Dictionary output:
2016-07-03 22:52:21.330 T2PApp[2272:658440] (
{
OrderDetailID = 3;
ProductName = Oranges;
UnitPrice = "399.99";
date = "2016-06-09T21:45:06";
},
{
OrderDetailID = 7;
ProductName = Oranges;
UnitPrice = 1000;
date = "2016-06-13T22:15:47.107";
}
)
Extracted UnitPrice output (still not completely extracted):
2016-07-03 22:52:21.330 T2PApp[2272:658440] Price: (
399.99,
1000
)
I think what you need is digging out the data from the objects in array. It not about the JSON.
There is many way to do it, but not one simply way to dig it out.
For example, create a new array, and traverse the target array, put the property you need into the new array.
It is basically like that.
In your code, maybe the code below will work.
NSLog(#"%#",JSON);
NSMutableArray *priceArr = [NSMutableArray array];
NSArray *arr = nil;
if ([JSON isKindOfClass:[NSArray class]]) {
arr = (NSArray *)JSON;
for (NSDictionary *dic in arr) {
NSString *price = [NSString stringWithFormat:#"%#",[dic valueForKey:#"UnitPrice"]];
price= [price stringByReplacingOccurrencesOfString:#"\"" withString:#""];
[priceArr addObject:price];
}
}
priceArr is what you need.

Parsing JSON with AFNetworking into NSDictionary

I am trying to parse some JSON with AFNetworking and an NSDictionary. However something seems strange with the JSON. The JSON contains routes for shuttles, but I do not see a 'route' in the JSON.
When I run this code I get an empty NSDictionary with 15 allocated spaces.
NSString *methodURL = [NSString stringWithFormat:#"%#GetRoutesForMapWithSchedule", BASE_URL];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:methodURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
_routes = (NSDictionary *)responseObject;
//_route = _routes[#"Stops"];
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Can anyone explain how I can get all the info for one route? This is the JSON.
NSArray *routes = (NSArray *)responseObject;
NSDictionary *oneRoute = responseObject[0];
NSDictionary *stops = oneRoute[#"Stops"];
The problem is that the top level JSON object is actually an array, not a dictionary as you're trying to cast it to. Each element of the array on the other hand, is a dictionary.
Here is some code to get the general data structure and to get a piece of data out of one of the routes.
NSArray *routes = (NSArray *)responseObject;
NSDictionary *firstRoute = responseObject[0];
NSString *description = firstRoute[#"Description"];
Edit: To parse Stops, you would do something like this:
NSArray *stops = firstRoute[#"Stops"];
NSDictionary *firstStop = stops[0];
NSString *stopDescription = firstStop[#"Description"];

What am I doing wrong with the Pocket API for Objective-C that's causing archive commands to fail?

I'm really scratching my head at this one. I'm using the Pocket API to allow users to archive Pocket articles from my app, but whenever I try to do so with the below code I get this error:
Error Domain=PocketSDK Code=400 "Invalid request, please refer to API documentation" UserInfo=0xc17d3b0 {NSLocalizedDescription=Invalid request, please refer to API documentation}
Code:
NSDictionary *arguments = #{#"action": #"archive",
#"item_id": articleID};
[[PocketAPI sharedAPI] callAPIMethod:#"send" withHTTPMethod:PocketAPIHTTPMethodPOST arguments:arguments handler:^(PocketAPI *api, NSString *apiMethod, NSDictionary *response, NSError *error) {
if (!error) {
NSLog(#"Archived article.");
}
}];
Exactly what part of that is incorrect? Am I not POSTing a send method to the API?
EDIT: I even changed it to have #"action" be #"actions" and to supply it the above NSDictionary, and it returns without an error but doesn't affect it on the Pocket website...
EDIT 2: Per the response of Joseph Chen I changed my code to the following:
// Create data to pass to the Pocket API (a JSON array of actions)
NSError *error;
NSArray *actions = #[#{#"action": #"archive",
#"item_id": articleID}];
NSData *actionsAsJSONData = [NSJSONSerialization dataWithJSONObject:actions options:kNilOptions error:&error];
NSString *actionsAsJSONString = [[NSString alloc] initWithData:actionsAsJSONData encoding:NSUTF8StringEncoding];
NSDictionary *arguments = #{#"actions": actionsAsJSONString};
[[PocketAPI sharedAPI] callAPIMethod:#"send" withHTTPMethod:PocketAPIHTTPMethodPOST arguments:arguments handler:^(PocketAPI *api, NSString *apiMethod, NSDictionary *response, NSError *error) {
if (!error) {
NSLog(#"%#", response);
}
else {
NSLog(#"%#", error);
}
}];
Which returns:
action_results" = (
1
);
status = 1;
Yet when I go to the website and log in, the article I "archived" is still staring me in the face, unarchived.
According to the documentation the actions parameter should be a JSON dictionary. So you could either...
Create the JSON dictionary manually:
NSString *jsonString = [NSString stringWithFormat:#"[{\"action\":\"archive\",\"item_id\":\"%#\"}]", articleID]; // articleID is a NSString?
NSDictionary *arguments = #{#"actions": jsonString};
Use NSJSONSerialization:
NSDictionary *actions = #{#"action": #"archive", #"item_id": articleID};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:actions
options:kNilOptions
error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSDictionary *arguments = #{#"actions": jsonString};
This answer is also a reference.
Here's the code taken (almost) straight from my app:
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];
NSDictionary *arguments = #{#"actions" : #[#{#"action" : #"archive",
#"item_id" : itemId,
#"time" : [NSString stringWithFormat:#"%ld", (long)timestamp]}]};
[self.pocketAPI callAPIMethod:#"send"
withHTTPMethod:PocketAPIHTTPMethodPOST
arguments:arguments
handler:^(PocketAPI *api, NSString *apiMethod, NSDictionary *response, NSError *error)
{
if (!error) {
// OK
} else {
// handle error
}
}];

With AFNetworking how can I read the error message contained in request.responseString

NSLog(#"%#", request.responseString);
This gives me output of {"errors":{"email":["is already taken"]}}.
I would like to save email and the message string "is already taken" into a string to display in an alert. How can I access those two items into two strings?
The response string is the raw output from the server. In this case it is JSON encoded. You can either use one of the AFNetworking JSON-specific classes (i.e. AFJSONRequestOperation) to get the response back as a JSON object, or parse it yourself using NSJSONSerialization. I would suggest using AFJSONRequestOperation.
NSData *data = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *str = [[json objectForkey:#"errors"] objectForKey:#"email"][0];
I used the following, seems a tiny bit more robust:
[requestOperation setCompletionBlockWithSuccess:success failure:^(AFHTTPRequestOperation *operation, NSError *error) {
id response = error.userInfo;
if (response && [response isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDictionary = (NSDictionary *)response;
// AFNetworking hides the actual error response under this key
if ([responseDictionary valueForKey:NSLocalizedRecoverySuggestionErrorKey]) {
id suggestedRecovery = [responseDictionary valueForKey:NSLocalizedRecoverySuggestionErrorKey];
if ([suggestedRecovery isKindOfClass:[NSString class]]) {
// Try to json decode string
id json = [NSJSONSerialization JSONObjectWithData:[suggestedRecovery dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
if (json && [json isKindOfClass:[NSDictionary class]]) {
responseDictionary = json;
}
}
}
// .. extract error message out of responseDictionary
}
}];
NSData *responseData = [[error userInfo] objectForKey:#"data"];
if ([responseData length] > 0)
{
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#", str);
}

how to send array as a parameter in Afnetwoking post method?

hi i need to send a array as a one of the parameter in Afnetworking Query String
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:#"http://192.008.0.28/aaa/a/"]];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: #"20", #"Miles", [NSArray arrayWithObjects:#"1",#"2",#"3",nil], #"Interval", nil];
[httpClient postPath:iUpdateNotificationMethod parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"Request Successful, response '%#'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[HTTPClient Error]: %#", error.localizedDescription);
}];
But server side we got "Miles":20,"Intervals":null how to fix it
Thanks,
Try This
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:OAuthBaseURL];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:0];
for (int i =0; i < [userIDs count]; i++) {
NSString *userID = [[userIDs objectAtIndex:i] objectForKey:#"id"];
NSDictionary *tmpDict = [NSDictionary dictionaryWithObjectsAndKeys:userID , [NSString stringWithFormat:#"ids[%i]",i], nil];
[parameters addEntriesFromDictionary:tmpDict];
}
[client postPath:#"/user"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSData *data = (NSData *)responseObject;
NSString *jsonStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(#"jsonStr %#",jsonStr);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self showError];
}
];
Since you're submitting an array, AFNetworking is generating a different parameter name and overloads it with the values you supply. For example, your request generates the following querystring:
Interval[]=1&Interval[]=2&Interval[]=3&Miles=20
This is defined in AFHTTPClient.m in the AFQueryStringPairsFromKeyAndValue function.
If you want to keep the original parameter, you should decide how to convert your NSArray to NSString by yourself. For example, you can do something like [myArray componentsJoinedByString:#","] and then split it back to elements on the server. If you choose this method, beware of using characters that might appear in your actual data.
I believe this will work:
params = #{ #"Miles": #"20", #"Interval": #[#"1",#"2",#"3"] };

Resources