2017-02-17 43 views
0

我最近開始爲我的Final Year Project學習移動開發,並使用Xamarin.Android和Azure移動服務開發Android應用程序。 我創建了一個測試數據庫並在那裏創建了一個條目,試圖使我的Android應用程序連接到數據庫並檢索此條目。我正在這樣做以瞭解如何建立連接和檢索數據,並從那裏開始正確修改。 這是我的模型類什麼樣子(注意,JSON屬性被命名完全一樣在我的數據庫中的列名。Azure with Xamarin.Android「您必須先登錄才能使用此應用程序」

using Newtonsoft.Json; 

namespace com.parkkl.intro.Models 
{ 
    class TestTable 
    { 
     public int Id { get; set; } 

     [JsonProperty(PropertyName = "UserName")] 
     public string UserName { get; set; } 
     [JsonProperty(PropertyName = "deleted")] 
     public bool Deleted { get; set; } 
     [JsonProperty(PropertyName = "version")] 
     public string Version { get; set; } 
     [JsonProperty(PropertyName = "createdAt")] 
     public string Creation { get; set; } 
     [JsonProperty(PropertyName = "updatedAt")] 
     public string Updated { get; set; } 
    } 
} 

這就是我的活動看起來怎麼樣

using Android.App; 
using Microsoft.WindowsAzure.MobileServices; 
using Android.OS; 
using com.parkkl.intro.Models; 
using System.Collections.Generic; 
using Android.Widget; 

namespace com.parkkl.intro 
{ 
    [Activity(Label = "ParkKL", MainLauncher = false, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     #region Variables 
     public static MobileServiceClient client = new MobileServiceClient 
      (@"http://parkkl.azurewebsites.net/"); 
     private IMobileServiceTable<TestTable> test = null; 
     private List<TestTable> testItems; 
     #endregion 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      test = client.GetTable<TestTable>(); 

      testItems = new List<TestTable>(); 

      GetData(); 

     } 

     public async void GetData() 
     { 
      var collection = await test.Where(user => user.Id == 1).ToCollectionAsync(); 

      foreach (var item in collection) 
      { 
       testItems.Add(
        new TestTable 
        { 
         Id = item.Id, 
         UserName = item.UserName, 
         Deleted = item.Deleted, 
         Version = item.Version, 
         Creation = item.Creation, 
         Updated = item.Updated, 
        }); 
      } 
      var finalItem = collection[0]; 
      TextView text = (TextView)FindViewById(Resource.Id.TextFromDB); 
      text.Append(finalItem.UserName); 
     } 
    } 
} 

現在的問題是,我每次嘗試部署的應用程序時,它拋出該異常

Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: 你必須先登錄在使用此應用程序

在我在Azure中的應用程序服務中,我已禁用所有身份驗證,仍然出現此錯誤。我想知道問題來自哪裏?!我會非常感謝你的幫助。

編輯:我想我發現了問題,這是表本身給出的權限。但是,我仍然找到一種方法來正確驗證我的應用程序

回答

0

有關如何處理身份驗證的詳細信息,請參閱本書http://aka.ms/zumobook

+0

謝謝阿德里安,我對這本書有所瞭解,它非常全面和清晰。它會幫助我很多。 我只是有一個問題,除此之外。通過使用Azure Web服務,將新用戶註冊到我的應用的最佳策略是什麼或什麼是最佳策略。這部分是在書中還是需要更多的研究?! 我真的很感謝分享這本書。 –

+0

這真的取決於你的情況 - 沒有「最好」的策略。因此,本書未涵蓋在內。 –

+0

謝謝阿德里安,其實我剛剛在這裏遇到了你的博客(https://shellmonger.com/30-days-of-azure-mobile-apps-the-table-of-contents/),說實話,真是令人難以置信的工作。我想我需要的只是在那裏,所以非常感謝你的幫助Adrian。 –

相關問題