Connecting to API on Local Host – Android Emulator

  android, android-emulator, api

I’ve tried connecting to an API running on local host to retrieve values on Android emulator.
I keep getting 404 Error. Any idea what should be done here for the API? Thank you for your help.

            var test = "";
            var url = "http://10.0.2.2:60002/api/values/" + Convert.ToInt32(Id);

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        "Basic", Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                               $"{user}:{pass}")));
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                var response = await client.GetStringAsync(url);
                test = response;
            }
            return test;

Source: Android Questions

LEAVE A COMMENT