Reputation: 73
I'm facing this error when I've to fetch data from web API from localhost, android app is running in emulator.
Failed to connect to localhost/127.0.0.1:44326
No response is getting from the localhost server in xamarin app whereas I'm able to fetch result in postman and in browser as well. I put URL ("https://localhost:44326/api/passportStatus") in xamarin MainPage.xaml.cs. are these info sufficient to know what I want actually?
This is my mainpage.xaml.cs
public partial class MainPage : ContentPage
{`public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
empDetails();
}
private async void empDetails()
{
var result = "";
HttpClient client = new HttpClient();
var API_URL = "https://localhost:44326/api/passportStatus";
client.BaseAddress = new Uri(API_URL);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage respons = await client.GetAsync(API_URL);
result = await respons.Content.ReadAsStringAsync();
var PassportDetails = JsonConvert.DeserializeObject<List<detail>>(result);
EmpDeatils.ItemsSource = PassportDetails;
}
}
Upvotes: 1
Views: 6824
Reputation: 169
On Visual Studio 2022 17.7+ version there is an option to create 'dev tunnels' which do exactly what conveyor/ngrok does.
Upvotes: 1
Reputation: 385
127.0.0.1 is localhost; it's the IP address of every local machine, that means you try to create a request from an Android App to an Android device
In another word, you try to connect with an Android device, not with a computer Localhost.
for that, you need to change the localhost in the computer from 127.0.0.1 to the current IP4 of your computer.
now try this extension and follow the instructions to do that in Visual studio: https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti
Upvotes: 1
Reputation: 21
check whether you have installed nuget package conveyor,if it is ok edit API URL as follows http://192.168.8.200:45455/ in yours question add 44326 insted of 45455
Upvotes: 2
Reputation: 197
Update your code like below
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
empDetails();
}
private async void empDetails()
{
var result = "";
HttpClient client = new HttpClient();
var API_URL = "https://192.168.0.47:44326/api/";//Updated
client.BaseAddress = new Uri(API_URL);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage respons = await client.GetAsync("passportStatus");//Updated
result = await respons.Content.ReadAsStringAsync();
var PassportDetails = JsonConvert.DeserializeObject<List<detail>>(result);
EmpDeatils.ItemsSource = PassportDetails;
}
}
Hope this will resolve ur issue
Upvotes: 1