如何在Xamarin.Forms项目中连接WebServiceURL?

时间:2016-08-04 08:28:00

标签: c# asp.net web-services xamarin xamarin.forms

大家好日子。我正在Visual Studio 2015中创建Xamarin.Forms Portable Application。

我希望我的移动应用程序连接到我在VS2015中的SQL数据库,并返回一个应该显示在我的手机上的客户列表。

在我的解决方案中,我创建了一个Xamarin Portable项目和一个WebForms项目,它将处理我的Web服务和数据库。

在我的WebForms项目中,我能够使用LINQ表达式从数据库中检索数据。我甚至使用 WEB API 检查了这一点,我真的能够检索数据。如下图所示:

enter image description here

我想要做的是使用RestClient将此数据访问到我的Xamarin Portable项目。我使用WebForms项目的WebServiceUrl来获取它包含的数据。但每当我使用断点来测试它是否检索到数据时,它都不会返回任何值。

意思是我无法真正展示客户名单。

您认为这背后的原因是什么?

以下是我的一些代码:

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));
    }

    }
}

注意:请仔细查看我的RestClient,看看我做得对不对。非常感谢。

1 个答案:

答案 0 :(得分:1)

我不认为这是模拟器或手机的问题。你不能通过Android设备连接到localhost。如果路由器连接到无线,则必须使用路由器提供的外部IP地址。请参阅这两个主题

How can I access my localhost from my Android device?

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