mark
mark

Reputation: 199

getting crash and not getting json data

i use to get data from this url api.but i checked this in my console but it getting crash.It like get the movie list and to display in table view.Here i use to check whether i am doing in correct way or in wrong manner.

Here my code:

EDITED:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *authUrl =@"http://www.omdbapi.com/?s=Game%20of%20Thrones&Season=1";

    NSString *str1 = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str1];
    NSData * data=[NSData dataWithContentsOfURL:url];

    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
    NSLog(@"json%@",json);
}

Upvotes: 0

Views: 105

Answers (2)

user3182143
user3182143

Reputation: 9599

If you want to use GET for getting response from server just you can try in following method

//First give your URL here
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL  URLWithString:@"http://www.omdbapi.com/?s=Game%20of%20Thrones&Season=1"]];

//Set GET method for getting Response
[request setHTTPMethod:@"GET"];

//setValue and HTTPHeaderField here
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;

NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&err];

//Now you need to check response.Once you get the response,copy that and paste in ONLINE JSON VIEWER(google this or type online json viewer in google). 

//Paste the response and click the Viewer.

//Set whether it is DICTIONARY(if it starts with {}json) or ARRAY(if it starts with []json),depends upon the json result.

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

           //OR           

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

Upvotes: 1

Maulik Patel
Maulik Patel

Reputation: 397

I Got Your solution.

Just Replace my code in your code.

And in console you saw result..

 NSString *authUrl =@"http://www.omdbapi.com/?s=Game of Thrones&Season=1";

NSString *str1 = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:str1];
NSData * data=[NSData dataWithContentsOfURL:url];

NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSLog(@"json%@",json);

In Console

json{
    Search =     (
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BNTgxOTI4NzY2M15BMl5BanBnXkFtZTgwMjY3MTM2NDE@._V1_SX300.jpg";
            Title = "Game of Thrones";
            Type = series;
            Year = "2011\U2013";
            imdbID = tt0944947;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMjE5NTk5NDg3OV5BMl5BanBnXkFtZTgwNDExNzg2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones";
            Type = game;
            Year = 2012;
            imdbID = tt2231444;
        },
                {
            Poster = "N/A";
            Title = "Game of Thrones: A Telltale Games Series";
            Type = game;
            Year = 2014;
            imdbID = tt3391176;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTU1MzU2MDE4MV5BMl5BanBnXkFtZTgwNTc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Season 2 - Character Profiles";
            Type = movie;
            Year = 2013;
            imdbID = tt2653342;
        },
                {
            Poster = "N/A";
            Title = "Game of Thrones: A Day in the Life";
            Type = movie;
            Year = 2015;
            imdbID = tt4437700;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTc5MjM4OTkzMV5BMl5BanBnXkFtZTgwMzc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Season 2 - Invitation to the Set";
            Type = movie;
            Year = 2012;
            imdbID = tt2653340;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTYyODg1MjYzMV5BMl5BanBnXkFtZTgwOTc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Costumes";
            Type = movie;
            Year = 2011;
            imdbID = tt2653350;
        },
                {
            Poster = "N/A";
            Title = "Game of Thrones: You Win or You Die - Inside the HBO Series";
            Type = movie;
            Year = 2012;
            imdbID = tt2972984;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTQ1NzY5NDE1Ml5BMl5BanBnXkFtZTgwODc3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Season 2 - How to Be an Extra";
            Type = movie;
            Year = 2012;
            imdbID = tt2653338;
        },
                {
            Poster = "http://ia.media-imdb.com/images/M/MV5BMTgzNjYxNjY3NF5BMl5BanBnXkFtZTgwMTg3NzA2MDE@._V1_SX300.jpg";
            Title = "Game of Thrones: Inside the Night's Watch";
            Type = movie;
            Year = 2011;
            imdbID = tt2653352;
        }
    );
}

Upvotes: 1

Related Questions