1

I'm using Xamarin Forms to consume REST Api from NetFlix but i get this issue in Popup: System.Net.WebException: Error: NameResolutionFailure

Why o get this error?

My Code:

private HttpClient client = new HttpClient();
    private List<Movie> movies;

    public async Task<List<Movie>> LocalizaFilmesPorAtor(string ator)
    {

        if (string.IsNullOrWhiteSpace(ator))
        {
            return null;
        }
        else
        {
            string url = string.Format("http://netflixroulette.net/api/api.php?actor={0}", ator);
            var response = await client.GetAsync(url);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                movies = new List<Movie>();
            } else
            {

                var content = await response.Content.ReadAsStringAsync();
                var _movies = JsonConvert.DeserializeObject<List<Movie>>(content);
                movies = new List<Movie>(_movies);
            }

            return movies;
        }
    }

In debug mode said the error is in this code

string url = string.Format("http://netflixroulette.net/api/api.php?actor={0}", ator);
            var response = await client.GetAsync(url);

He stops in there, the url recive the url + actor name but in next line the response stay null.

PS: I give Internet permission to my App in Manifest!

Gabriel Gomes
  • 139
  • 1
  • 3
  • 20

3 Answers3

0

nuget packages: Microsoft HTTP Client Libraries and Newtonsoft.Json.

try this:

private HttpClient client = new HttpClient();
private List<Movie> movies;

public async Task<List<Movie>> LocalizaFilmesPorAtor(string ator)
{

    if (string.IsNullOrWhiteSpace(ator))
    {
        return null;
    }
    else
    {
     var client = new HttpClient(); 
     client.BaseAddress = new Uri("http://netflixroulette.net/");
     HttpResponseMessage response = client.GetAsync("api/api.php?actor={0}", ator);
        if(response.IsSuccessStatusCode)
        {
         var json = response.Content.ReadAsStringAsync().Result;
         var _movies = JsonConvert.DeserializeObject<List<Movie>>(json);
         movies = new List<Movie>(_movies);

        }
        else
        {

            movies = new List<Movie>();
        }

        return movies;
    }
}

PS: if movies = new List(_movies) not work, try foreach.

0

I had this issue too because my URL was malformed. Double check if your URL is correct with Postman/Browser.

maracuja-juice
  • 994
  • 2
  • 14
  • 33
-1

Why cant you try Refit? Refit is a library heavily inspired by Square's Retrofit library, and it turns your REST API into a live interface

What you just need to do is:

  1. Add Refit from Nuget Package
  2. Create an Interface with any name
  3. Import the Refit (Using Refit)

here is a sample code for the interface

public interface ISampleName
{

    [Get("api/api.php?actor={ator}")]
    async Task<List<Movie>> LocalizaFilmesPorAtor(string ator);
}

After that, then you can call it this way:

var SampleNameApi = RestService.For<ISampleName>("http://netflixroulette.net/");

var response= await SampleNameApi.LocalizaFilmesPorAtor("Sample");

I believe this will help you.

For More Information https://github.com/paulcbetts/refit