안녕하세요. Xamarin.Forms Portable Application을 만들고 있습니다.휴대 전화로 WebService 접속하기
내 모바일 응용 프로그램을 VS2015에있는 SQL 데이터베이스에 연결하고 휴대 전화에 표시해야하는 고객 목록을 반환하고 싶습니다.
내 솔루션에서는 웹 서비스 및 데이터베이스를 처리 할 Xamarin Portable 프로젝트와 WebForms 프로젝트를 만들었습니다.
문제는 입니다. 내 웹의 데이터에 액세스 할 수 없습니다. 내 모바일 장치에 프로젝트입니다.
내가 웹폼 프로젝트에 가지고있는 WebServiceUrl 또한 고객의 내 목록을 표시하기 위해 가정 내 RestClient.cs에서 사용하는 링크입니다 http://localhost:50857/api/Customer/
입니다. 내 노트북과 모바일이 동일한 WiFi에 연결되어 있습니다.
로컬 호스트를 192.168.1.11 인 로컬 호스트로 변경 했는데도 모바일에 액세스 할 수 없었습니다.
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;
}
}
}
:
은 여기 내 데이터베이스에서 데이터를 얻기를 처리 CustomerController 프로젝트에 대한 내 코드입니다.
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;
}
}
}
CustomerViewModel.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.cs 내 코드입니까? 도와주세요. 고마워.
당신은 모바일 브라우저에서 직접 IP에 액세스 할 수 있습니까? –
@Rohit the 'localhost'여기에 선생님은 내 노트북의 IP 주소로 바꾼 사람입니다.그래서 "http : // localhost : 50857/api/Customer /"; 'http://192.168.1.11:50857/api/ 고객'으로 변경했습니다. 하지만 여전히 웹 서비스에 액세스 할 수 없습니다. –
방화벽이 작동하도록 설정하고 방화벽에서 인바운드 규칙을 생성해야 할 수도 있습니다. 당신이 그것을 google하는 경우에 그것을하는 방법을 설명해 다수 기사가있다. 다음은 그 중 하나입니다 : [Visual Studio 2015 Android Emulator에서 Localhost Web API 호출하기] (http://briannoyesblog.azurewebsites.net/2016/03/06/calling-localhost-web-apis-from-visual-studio-android-emulator/). – jsanalytics