Give data from facebook and show it in array with JSON - iphone

I want get image from public perfil of Facebook and show it in my iPhone. I'm using the Facebook Developer method "graph.facebook.com" and later i want read it with JSON, but when i save data in NSArray, it SIGABRT.
NSURL *myURL = [NSURL URLWithString:url];
NSData *datosAlbum = [[NSData alloc] initWithContentsOfURL:myURL];
NSError *error=nil;
NSString *datosString=[[NSString alloc] initWithData:datosAlbum encoding:NSUTF8StringEncoding];
[datosAlbum release];
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSLog(#"%#",[parser objectWithString: datosString error: &error]);
NSArray *datos_array= [[NSArray alloc]initWithArray:[parser objectWithString: datosString error: &error]];
Can someone help me?
Thanks for all and Merry Christmas!
Best Regards!

Try: https://graph.facebook.com/4/picture
and for the json https://graph.facebook.com/4

Related

How to separate JSON response in iphone

I have an application in which I am having a json response like this.
{"success":"true","message":"You have logged in","pserial":"1"} and I am separating with ":".
And I am getting data like this pSerial:"1"} but I want only 1 value.
NSURL *url = [NSURL URLWithString:strUrl];
NSData *respData = [NSData dataWithContentsOfURL:url];
NSString *strResp = [[NSString alloc]initWithData:respData encoding:NSUTF8StringEncoding];
NSString *approvalString = [[strResp componentsSeparatedByString:#":"] objectAtIndex:3];
NSLog(#"pSerial:%#",approvalString);
for Example :
SBJsonParser *jsonPar = [[SBJsonParser alloc] init];
NSError *error = nil;
NSArray *jsonObj = [jsonPar objectWithString:jsonString error:&error];
id jsonObj = [jsonPar objectWithString:jsonString error:&error];
if ([jsonObj isKindOfClass:[NSDictionary class]])
// treat as a dictionary, or reassign to a dictionary ivar
else if ([jsonObj isKindOfClass:[NSArray class]])
// treat as an array or reassign to an array ivar.
Then get the value :
NSMutableArrary *userMutArr = [NSMutableArray array];
for (NSDictionary *dict in jsonObj)
{
User *userObj = [[[User alloc] init] autorelease];
[userObj setFirstName:[dict objectForKey:#"firstName"]];
[userObj setLastName:[dict objectForKey:#"lastName"]];
[userObj setAge:[[dict objectForKey:#"age"] intValue]];
[userObj setAddress:[dict objectForKey:#"address"]];
[userObj setPhoneNumbers:[dict objectForKey:#"phoneNumber"]];
[userMutArr addObject:userObj];
}
Hope you will understand. and read some Documents. it will help you.
It looks like you are in need of JSON Parsing. Here, what you want is JSON Parsing, not separating data from the JSON Response. JSON is the format of the data in which data is formatted in Key-Value pairs. You can fetch the "Value" of any object using the "Key".
Your first two lines are correct.
NSURL *url = [NSURL URLWithString:strUrl];
NSData *respData = [NSData dataWithContentsOfURL:url];
Now, you can parse JSON Response like this :
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:respData options:kNilOptions error:&error];
NSString *pSerial = [json objectForKey:#"pserial"];
This will give you the value of "pserial" from your response. Similarly, you can get the values for "success" and "message". You can check it using this line :
NSLog(#"pserial :: %#",pserial);
You need to parse the JSON Response String, you can use any JSON parser like:
https://github.com/stig/json-framework/
And in your code do:
NSString *strResp = [[NSString alloc]initWithData:respData encoding:NSUTF8StringEncoding];
NSDictionary *ResponseDictionary = [strResp JSONValue];
NSString * pSerial = (NSString*)[ResponseDictionary objectForKey:#"pserial"];
Dont separation by ":" just use JSONValue your response is like
// {"success":"true","message":"You have logged in","pserial":"1"}
// with SBJsonParser parse your object like this
NSDictionary *responseJson = [YOUR-OBJECT JSONValue];
Note: dont forget to add Json header file
Its better you use any OpenSource Json Parser
Here is a stack post Comparison of different Json Parser for iOS

CHCSVParser - how to load from webserver

I would like to parse csv from webserver which gets updated everyday.I am using the csvparser from this link https://github.com/davedelong/CHCSVParser and I am using this code:
NSError *err = [[[NSError alloc] init] autorelease];
NSString *lunchFileURL = [[NSString stringWithFormat:#"http://www.somewhere.com/LunchSpecials.csv"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *lunchFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:lunchFileURL] encoding:NSUTF8StringEncoding error:&err];
CHCSVParser *p = [[CHCSVParser alloc] initWithContentsOfCSVString:lunchFile usedEncoding:&encoding error:nil];
I get this error :
No visible #interface for 'CHCSVParser' declares the selector 'initWithContentsOfCSVString:usedEncoding:error:'
I checked this link Load remote csv into CHCSVParser and its not working .I am a noob to ios ,Please let me know how to fix this .Really Appreciate the help.Thanks in Advance.
It should probably be:
NSError *err;
NSString *lunchFileURL = [[NSString stringWithFormat:#"http://www.somewhere.com/LunchSpecials.csv"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *lunchFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:lunchFileURL]
encoding:NSUTF8StringEncoding
error:&err];
// if you're going to create a CHCSVParser yourself, the syntax is:
CHCSVParser *p = [[CHCSVParser alloc] initWithCSVString:lunchFile
encoding:NSUTF8StringEncoding
error:&err];
// or, if you're going to use NSArray+CHCSVAdditions.h, the syntax might be:
NSArray *array = [[NSArray alloc] initWithContentsOfCSVString:lunchFile
encoding:NSUTF8StringEncoding
error:&err];
Note:
You don't need to alloc/init the NSError object; these classes that take a NSError ** parameter will create the autorelease error object for you if they encounter an error; otherwise they leave it alone;
The CHCSVParser class method is not initWithContentsOfCSVString, but rather initWithCSVString;
Alternatively, if you use the NSArray class extension, then the syntax is arrayWithContentsOfCSVString or initWithContentsOfCSVString; and
You specified an encoding of &encoding, but this parameter is not a pointer, so I don't see how that can possibly be right; I've just specified the encoding.
I assume you want to use the NSArray+CHCSVAdditions category method initWithContentsOfCSVString or arrayWithContentsOfCSVString (which gives you an autorelease object), not the CHCSVParser, but it's up to you.

How would i take multiple url's out of a string?

I am having my server send me a list of all the images in a certain users folder, it sends me a string and i am using SBJsonparser to get it. here is the coding:
NSString *url = [[NSString alloc]initWithFormat:#"http://mysite.com/members/grabimages.php?&username=%#", [getLogin objectAtIndex:0]];
NSString *connect = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:url]];
SBJsonParser *parser = [[SBJsonParser alloc]init];
NSString *imagenames = [[NSString alloc]init];
imagenames = [parser objectWithString:connect error:nil];
NSLog(#"%#", imagenames);
The string returns this:
(
"./username/default/img1.jpg",
"./username/default/img2.jpg"
)
There is going to be lots of URL's in this one string at some point, i want to be able to separate all the URL's and then add each one of them into a new string.
How would i be able to do this?
Thanks :)
the error from the other post error says the object was an NSMutableArray. presumably, that object was imagenames.
if so, then you declare it like so:
NSArray *imagenames = [parser objectWithString:connect error:nil];
then you use your relative base url to compose the url, as they are relative to some directory you must know of. so, you would compose the base (as an NSURL), and use one of the -[NSURL URLByAppending... methods to create new URLs from the elements of imagenames.
You could use NSArray *array = [imagenames componentsSeparatedByString:#","]; to separate the the urls

Travel Application Airport code returns City,Country

I am trying to develop an travel application.I would like to submit the Airport code and get back the Country,City, along with the weather of that particular space.Is there an API out in the market that will help me in doing that???Or is there some other way I can do that?
Here's one for the city/country: http://airportcode.riobard.com/about
and this question has good info about weather: Weather API - Provides "About to..." Information?
Here's a sample piece of code that accepts the text from 'inputTextField' and outputs the location to 'outputLabel'
-(IBAction)getCity{
NSString *urlString = [NSString stringWithFormat:#"http://airportcode.riobard.com/airport/%#?fmt=json",self.inputTextField.text];
NSURL *url = [NSURL URLWithString:urlString];
NSString *response = [[NSString alloc] initWithContentsOfURL:url];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSDictionary *airport = [responseString JSONValue];
self.outputLabel.text=[airport objectForKey:#"location"];
}

canonical way to read plist from URL into NSDictionary? How to control timeout?

I have a servlet that serves up a plist XML file. What's the best way to slup this into an NSDictionary? I have this basically working with:
NSDictionary* dict = [ [ NSDictionary alloc] initWithContentsOfURL:
[NSURL URLWithString: #"http://example.com/blah"] ];
But then I have no control over the timeout; I'd rather not have my UI hang for 60 seconds just because the server (which I may not control) is having a hissy fit. I know about NSURLRequest, which will let me do the following:
NSURLRequest *theRequest=[NSURLRequest requestWithURL:
[NSURL URLWithString: #"http://example.com/blah"
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:5 ];
But I don't quite see how to feed that to NSDictionary.
You need to do this using the asynchronous methods. Start with this answer/post:
Can I make POST or GET requests from an iphone application?
This will get you your data and place it in a varibale: responseData. Now you need to add your code to convert it to a NSDictionary in the following method:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
I've found 2 possible ways to convert NSData to NSDictionary. This way is straight from the Apple Archives and Serializations Guide.
NSString *errorStr = nil;
NSPropertyListFormat format;
NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData: responseData
mutabilityOption: NSPropertyListImmutable
format: &format
errorDescription: &errorStr];
Second:
NSString *string = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
NSDictionary *dictionary = [string propertyList];
[string release];