Android App Connecting to LocalHost

Hello,

I have started learning Xamarin in August, and now i am really liking it and i got a project to do. So, lets go to the problem i have:

Basically i am following this tutorial :

I am using asp.net and REST web service.

Now the problem is that he is using windows phone and windows phone connects to the localhost but not the android phone. So when i try to get some data by using the REST class i have here and trying to display it in a listview it doesn't get anything. The problem is it doesn't connect, so i changed it with the IP and the port which i got from this tool: https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

Now when i open Chrome in my phone and i use that IP and PORT it displays it like in laptop (they are in the same network) but when i use it in a string webservice url in the REST Class to convert it to json it doesn't get the data.

My REST CLASS:

The code is completely the same as the one in Tutorial cause i rechecked it many times.

Thanks in advance, and sorry if the question isn't good formulated cause it is mine first post here.

Tagged:

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello @IrvinBoy
    since many month i've try to connect my android phone to my localhost but actually impossible ...
    For to resolve this problem because i've read some solution on internet but actually nothing work ...

    well for to resolve it, i've try some sample on internet for to get some experience with json , and so on
    you can try this for example the pizzaboy json :) there a lot of url where you can access OF COURSE it's not your own backend but for to learn that's nice i think ...

    `
    async Task DownloadPizzaMenu()
    {
    var content = "";

            var uri = new Uri("http://www.pizzaboy.de/app/pizzaboy.json");
            client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
    
            try
            {
                var response = await client.GetAsync(uri);
                if (response.IsSuccessStatusCode)
                {
                    content = await response.Content.ReadAsStringAsync();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
    

    `
    another solution will be to expose your web service in internet ...

    Wait perhpas some pro will be the solution

  • CorwynCorwyn USMember ✭✭

    @IrvinBoy it is not clear to me how you are determining that your web service is never called. So, to address one particular issue folks run into, you may want to read up on the async/await paradigm here
    https://developer.xamarin.com/guides/cross-platform/advanced/async_support_overview/

    But in particular you keep the following in mind:
    "Using await does not block the caller’s thread – rather control is returned to the caller. This means that the calling thread is not blocked, so for example the user interface thread would not be blocked when awaiting a task.

    When the task completes, the method resumes executing at the same point in the code. This includes returning to the try scope of a try-catch-finally block (if one is present). await cannot be used in a catch or finally block."

  • TrevorBalcomTrevorBalcom USMember ✭✭

    It looks like you're using the Google Emulator. You can use the IP address 10.0.2.2 to connect to the hosting PC. Try changing your URL to http://10.0.2.2/api/Employees. Use the browser provided by the emulator to establish if you can connect or not. If you can't connect with that IP address then you may need to reconfigure your firewall.

  • IrvinBoyIrvinBoy ALMember ✭✭

    @ChristopheBERNARD said:
    Hello @IrvinBoy
    since many month i've try to connect my android phone to my localhost but actually impossible ...
    For to resolve this problem because i've read some solution on internet but actually nothing work ...

    well for to resolve it, i've try some sample on internet for to get some experience with json , and so on
    you can try this for example the pizzaboy json :) there a lot of url where you can access OF COURSE it's not your own backend but for to learn that's nice i think ...

    `
    async Task DownloadPizzaMenu()
    {
    var content = "";

            var uri = new Uri("http://www.pizzaboy.de/app/pizzaboy.json");
            client = new HttpClient();
            client.MaxResponseContentBufferSize = 256000;
    
            try
            {
                var response = await client.GetAsync(uri);
                if (response.IsSuccessStatusCode)
                {
                    content = await response.Content.ReadAsStringAsync();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
    

    `
    another solution will be to expose your web service in internet ...

    Wait perhpas some pro will be the solution

    Thanks, but i think i will wait a bit more and see if i can get a solution, if not then i will move to this https://developer.xamarin.com/samples/xamarin-forms/WebServices/TodoASMX/.>;

    @Corwyn said:

    @IrvinBoy it is not clear to me how you are determining that your web service is never called. So, to address one particular issue folks run into, you may want to read up on the async/await paradigm here
    https://developer.xamarin.com/guides/cross-platform/advanced/async_support_overview/

    But in particular you keep the following in mind:
    "Using await does not block the caller’s thread – rather control is returned to the caller. This means that the calling thread is not blocked, so for example the user interface thread would not be blocked when awaiting a task.

    When the task completes, the method resumes executing at the same point in the code. This includes returning to the try scope of a try-catch-finally block (if one is present). await cannot be used in a catch or finally block."

    I am debugging it and when it goes to the json var i keep steping over to the methods but all the data they return is null. So, normally it doesn't show anything on the list view i have. Thanks for the response and i will read about the async/await paradigm.

    @TrevorBalcom said:
    It looks like you're using the Google Emulator. You can use the IP address 10.0.2.2 to connect to the hosting PC. Try changing your URL to http://10.0.2.2/api/Employees. Use the browser provided by the emulator to establish if you can connect or not. If you can't connect with that IP address then you may need to reconfigure your firewall.

    Yeah i tried it but it just keeps loading forever until it times out. I tried it like this too 10.0.2.2:45455 with the port but still it isn't working. Thanks for the reply.

  • IrvinBoyIrvinBoy ALMember ✭✭

    Now the Emulator in the browser when i use the IP it finds the employees. I changed the string in visual studio to the same ip and port but it still doesnt get that xml code.

Sign In or Register to comment.