0

Good Day Everyone. I'm creating Xamarin.Forms Portable Application in my Visual Studio 2015.

I want my Mobile Application to connect to the SQL Database I have in my VS2015 and return a LIST OF CUSTOMERS which should be display to my mobile phone.

In my solution, I have created a Xamarin Portable project and a WebForms project that will handle my Web Services and Database.

In my WebForms project, I was able to retrieve data from the Database using LINQ expression. I even checked this by using WEB API and I was really able to retrieved data. Like the image below :

enter image description here

What I want to do is to access this data to my Xamarin Portable project using RestClient. I used the WebServiceUrl of my WebForms project to get the data it contains. But whenever I used a Breakpoint to test if it retrieved the Data, it does not return any value.

Meaning I won't really be able to display the LIST OF CUSTOMERS.

What do you think is the reason behind this?

Here are some of my codes :

1.) WebForms

CustomerController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebFormsDemo;
using WebFormsDemo.ViewModel;

namespace WebFormsDemo.Controllers
{
    public class CustomerController : ApiController
        {
    private EBMSEntities db = new EBMSEntities();

    // GET: api/Customer
    public IQueryable<CustomerViewModel> GetCustomerViewModels()

    {
        var displaycustomerInfo = from cust in db.CUSTOMERs
                                  select new CustomerViewModel
                                  {
                                      Id = cust.CUSTOMER_ID,
                                      CUSTOMER_CODE = cust.CUSTOMER_CODE,
                                      CUSTOMER_NAME = cust.CUSTOMER_NAME,
                                      CUSTOMER_MOBILE_NUMBER = cust.CUSTOMER_MOBILE_NUMBER,
                                      CUSTOMER_EMAIL_ADDRESS = cust.CUSTOMER_EMAIL_ADDRESS,
                                      CUSTOMER_CONTACT = cust.CUSTOMER_EMAIL_ADDRESS + "," + " " + cust.CUSTOMER_MOBILE_NUMBER
                                  };




        return displaycustomerInfo;
        }

    }
}

2.) XamarinForms

RestClient.cs

public class RestClient_Customer <T>
{


    private const string WebServiceUrl = "http://localhost:50857/api/Customer/";

    public async Task<List<T>> GetCustomerAsync()
    {
        var httpClient = new HttpClient();

        var json = await httpClient.GetStringAsync(WebServiceUrl);

        var taskModels = JsonConvert.DeserializeObject<List<T>>(json);

        return taskModels;
    }
  }

.

CustomerServices.cs

using Plugin.RestClient;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using XamarinFormsDemo.Models;

namespace XamarinFormsDemo.Services
{
    public class CustomerServices
    {

    public async Task<List<Customer>> GetCustomerAsync()
    {
        RestClient_Customer<Customer> restClient = new RestClient_Customer<Customer>();

        var customerList = await restClient.GetCustomerAsync(); //yung getasync ay pantawag as restclient

        return customerList;
        }
    }
}

.

CustomerVM.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using XamarinFormsDemo.Models;
using XamarinFormsDemo.Services;
using XamarinFormsDemo.Views;

namespace XamarinFormsDemo.ViewModels
{
public class CustomerVM : INotifyPropertyChanged
{


    private List<Customer> _customerList; // keep all customers
    private List<Customer> _searchedCustomerList; // keep a copy for searching
    private Customer _selectedCustomer = new Customer();

    private string _keyword = "";
    public string Keyword
    {
        get
        {
            return _keyword;
        }
        set
        {
            this._keyword = value;

            // while keyword changed we filter Employees
            //Filter();
        }
    }



    private void Filter()
    {
        if (string.IsNullOrWhiteSpace(_keyword))
        {
            CustomerList = _searchedCustomerList;

        }
        else
        {
            // var lowerKeyword = _keyword.ToLower();
            CustomerList = _searchedCustomerList.Where(r => r.CUSTOMER_NAME.ToLower().Contains(_keyword.ToLower())).ToList();
            //  EmployeesList = _searchedEmployeesList.Where(r => r.EMPLOYEE_NAME.Contains(_keyword)).ToList();


        }
    }




    public List<Customer> CustomerList
    {
        get
        {
            return _customerList;
        }
        set
        {
            _customerList = value;
            OnPropertyChanged();
        }
    }


    public CustomerVM()
    {
        InitializeDataAsync();

    }

    private async Task InitializeDataAsync()
    {
        var customerServices = new CustomerServices();
        _searchedCustomerList = await customerServices.GetCustomerAsync();
        CustomerList = await customerServices.GetCustomerAsync();

    }




    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    }
}

NOTE: Please take a closer look to my RestClient whether I'm doing it right thing or not. Thanks a lot.

Jaycee Evangelista
  • 1,107
  • 6
  • 22
  • 52

1 Answers1

1

I dont think it is the problem with emulator or phone. You cant just connect to localhost over your android device. you must use the external ip address provided by the router if it is connected to Wireless. Please see these 2 topics

How can I access my localhost from my Android device?

How to connect to my http://localhost web server from Android Emulator in Eclipse

Community
  • 1
  • 1
Emil
  • 6,411
  • 7
  • 62
  • 112
  • but I'm connecting Sir via WebService? I get the WebServiceURL of my web forms project just like this : "http://localhost:50857/api/Customer/"; and access it in RestClient. Do I still need to modify something to do it? – Jaycee Evangelista Aug 04 '16 at 09:48
  • @JayceeEvangelista your Webservice needs to be reachable outside of your local pc. Can you connect your Webservice from another computer? If no, it is the same thing Android device cant connect as well. – Emil Aug 04 '16 at 09:53
  • how will I know if I can access the WebService from another pc? Should I just access this? http://localhost:50857/api/Customer/ – Jaycee Evangelista Aug 04 '16 at 10:01
  • @JayceeEvangelista yes if it is http get – Emil Aug 04 '16 at 10:05
  • Sir I use that link but wasn't able to access it's content. What does this mean? – Jaycee Evangelista Aug 04 '16 at 10:17
  • I tried changing the WebServiceUrl from localhost:50857/api/Customer to http://192.168.1.11:50857/api/Customer/ where I replaced the localhost to the ip adderess of my pc. – Jaycee Evangelista Aug 04 '16 at 10:33
  • @JayceeEvangelista Are you able to call it from another computer? if not, you still have a problem to access to your webservice externally. – Emil Aug 04 '16 at 10:37
  • No Sir. I tried to access in another pc this link : 192.168.1.11:50857/api/Customer but still wasn't able to see anything. – Jaycee Evangelista Aug 04 '16 at 10:57
  • @JayceeEvangelista thats why you should Google around a bit how to Access Webservice externally. Your Problem is not xamarin or Android but Webservice apperantly – Emil Aug 04 '16 at 11:05
  • Alright Sir. Thanks for your advise. What should be my main problem if I wasn't able to access the web service of my pc to my mobile? – Jaycee Evangelista Aug 04 '16 at 11:10