![](/img/trans.png)
[英]How to solve error in windows phone 8 application when retrieving app from background with web services?
[英]Access web api 2 app from windows phone 8 application
當我嘗試從Windows Phone 8應用程序訪問在本地主機上運行的ASP.NET Web Api 2應用程序時遇到問題。 我已經嘗試並尋找了很多示例,但沒有結果。 我使用Fiddler測試了我的api,它起作用了。 我測試過可以從Web應用程序訪問它,但是它可以工作,但是當我嘗試從Windows Phone 8應用程序訪問它時卻不能。 您能告訴我如何配置Windows Phone 8仿真器來訪問它嗎? 先感謝您。
仿真器是虛擬機,因此電話應用程序中的“ localhost”表示仿真器本身,而不是運行Web服務的主機。 要訪問它,您必須在局域網中提供主機的實際IP地址,而不是“ localhost”。 您可以通過在cmd控制台中運行ipconfig來查看IP。
這是有關在Windows Phone 8中訪問Web api方法的詳細文章
具體而言,這是如何從Web API獲取數據
string apiUrl = @"http://www.contoso.com/api/Books";
WebClient webClient = new WebClient();
webClient.Headers["Accept"] = "application/json";
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadCatalogCompleted);
webClient.DownloadStringAsync(new Uri(apiUrl));
private void webClient_DownloadCatalogCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
this.Items.Clear();
if (e.Result != null)
{
var books = JsonConvert.DeserializeObject<BookDetails[]>(e.Result);
int id = 0;
foreach (BookDetails book in books)
{
this.Items.Add(new ItemViewModel()
{
ID = (id++).ToString(),
LineOne = book.Title,
LineTwo = book.Author,
LineThree = book.Description.Replace("\n", " ")
});
}
this.IsDataLoaded = true;
}
}
catch (Exception ex)
{
this.Items.Add(new ItemViewModel()
{
ID = "0",
LineOne = "An Error Occurred",
LineTwo = String.Format("The following exception occured: {0}", ex.Message),
LineThree = String.Format("Additional inner exception information: {0}", ex.InnerException.Message)
});
}
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.