I'm loading json with afnetworking 2.0:
NSString *weatherUrl = #"http://www.souche.com/pages/xx/xx.json?request_message={\"type\":\"car-subdivision\"}";
weatherUrl = [weatherUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:weatherUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
console error:
'Invalid parameter not satisfying: URLString'
Is there need url encoding?
but i do it like this:
NSString *weatherUrl = #"http://www.souche.com/pages/dicAction/loadRootLevel.json?request_message={%22type%22:%22car-subdivision%22}";
still error!
How can i do?
It looks like there are a number of issues with the URL you're constructing, and the way you're passing (or not passing) parameters into AFNetworking. You don't need to construct your query string yourself, as AFNetworking will do that for you. As mentioned in my comment above, passing query=where UserName='abc' as part of a URL seems like a bad idea. However, here's a quick example of how you'd call AFNetworking's GET method if your URL was slightly different:
URL format:
https:////?username=abc&companyId=&page=1&pageSize=25&filterResultByColumns=true
you try this code :D
Related
I have to send a JSON - with device id and type - to API, and it will give me another JSON with token and few other things.
I'm fairly new to iOS, it has been three weeks and I'm really new to web services. So, I couldn't figure out how to both send and get a JSON.
I'm trying to use AFNetworking at the same time, which is in my opinion the simplest way. So, I thought it might be good to use POST to send a JSON and I achieved with the following code:
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];//DeviceID
NSLog(#"unique idf: %# and type: %#", uniqueIdentifier, [[NSNumber numberWithInt:1] stringValue]);
NSURL *baseURL = [NSURL URLWithString:BaseURLString];
NSDictionary *parameters = #{#"DeviceId" : uniqueIdentifier, #"Type" : [[NSNumber numberWithInt:1] stringValue]};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:BaseURLString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
So, it returns me the device id and type, I know from writing to log. However I stuckted there. On the other hand, it doesn't reach to the success block. I debuged it right now, and also no printing on log screen although there was a command NSLog for it. What might be problem?
It's just confusing to construct a GET there, for me, honestly. So, I would be really appreciated if you can help me to figure out that situation.
Thanks.
Inside your success block do this -
[self performSelector:#selector(makeGetRequest:) withObject:responseObject];
Write the GET functionality in "makeGetRequest" method
-(void)makeGetRequest:(id)sender {
NSLog(#"responseObject - %#",sender);
// Write GET functionality here
}
I am trying to get a request token from Yahoo by making a POST request to the following URL:
https://api.login.yahoo.com/oauth/v2/get_request_token
I am making the request in the following way:
int randomNumber = rand();
NSDictionary *parameters = #{#"oauth_consumer_key": YahooConsumerKey,
#"oauth_signature_method": #"plaintext",
#"oauth_signature": YahooConsumerSecret,
#"oauth_version": #"1.0",
#"xoauth_lang_pref": #"en_us",
#"oauth_callback": #"oob",
#"oauth_timestamp": [self getTimeStamp],
#"oauth_nonce": [NSString stringWithFormat:#"%d", randomNumber]};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"https://api.login.yahoo.com/oauth/v2/get_request_token" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
The problem is that I keep getting the error 401 with the explanation that the consumer key is unknown. I have pasted the consumer key from where I've created my project, so I really don't know what I am doing wrong. Any help will be appreciated. Thanks!
Your request looks good. Try appending #"%26" to your YahooConsumerSecret string. Here's your request link with the fix, it seems to be working!
When using plaintext as oauth_signature_method you need to append %26 at the end of your oauth_signature according to the Yahoo docs.
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);
}];
I am sending a string to my server with AFNetworking and it works well. This is my code:
- (void)startWithContentString:(NSString *)contentString {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"string": #"Anne"};
[manager POST:#"http://xxxxx.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
If I replace #"Anne" by a more complicate string like #"--diner--1245366-tomorrow-2014-03-04--", it still works well.
What I want to do and have difficulties doing : Instead of putting the content of my string (#"xxxx"), I want it to be a variable string that I calculate myself depending on users answers, like :
NSString *myString =[NSString stringWithFormat:#"%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#%#",str11,str0a,str11,str0b,str11,str0bb,str11,str0c,str11,str0cc,str11,str1,str11,str2,str11,str3,str11,str4,str11,str5,str11,str6,str11,str7,str11,str8,str11];
but if i replace #"Anne" by myString, it doesn't work anymore.
Ive tried to avoid asking such a newb question on here, but im a Android dev learning IOS and I cant figure out for the life of me how to add a simple header to my post requests using AFNetworking 2.0. Below is my code so far which works if i want to make a request that doesnt require a header.Could anyone show me via adding to my snippet or providing a alternate one that does this? I came across this tut: http://www.raywenderlich.com/30445 that shows how to add a header under the "A RESTful Class" heading , but its for afnetworking 1.0 and is now depreciated as far as i can tell.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"uid": #"1"};
AFHT
[manager POST:#"http://myface.com/api/profile/format/json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(#"JSON: %#", responseObject);
self.feedArray = [responseObject objectForKey:#"feed"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
} ];
}
Under AFHTTPRequestOperationManager you will notice a property called requestSerializer. This is of type AFHTTPRequestSerializer, and requests made through the HTTP manager are constructed with the headers specified by this object.
So, try this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:#"SomeValue" forHTTPHeaderField:#"SomeHeaderField"]
//Make your requests
You can read the headers dictionary from the request serializer as follows:
manager.requestSerializer.HTTPRequestHeaders
Note that once you set a header this way, it will be used for all other operations!
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"staging" password:#"dustylendpa"];