2016-07-05 81 views
-1

任何幫助,非常感謝。我使用適用於Android Studio的Microsoft Azure Mobile SDK爲我的Android應用程序創建了後端。但是,當我嘗試從服務器使用查詢獲取數據時,該應用程序掛起並崩潰。我的代碼如下:當Azure SQL查詢產生時,Android應用程序崩潰

//The variables 
private MobileServiceClient mClient; 
private MobileServiceTable<Item> table; 

//In onCreate method 
try { 
    mClient = new MobileServiceClient("https://myapp.azurewebsites.net", this); 
} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} 
table = mClient.getTable("MyTable",Item.class); 
try { 
    final List<Item> result = table.execute().get();//----App crashes here 
} catch (MobileServiceException e) { 
    e.printStackTrace(); 
} 
+0

stacktrace說什麼? 「掛起和崩潰」包含完全零的細節。 – f1sh

+1

那麼如果這是主線程執行一個查詢並獲得像這樣的結果將導致NetworkOnMainThreadException。但是你沒有發佈堆棧跟蹤,所以我猜測。一般來說,儘管你不應該在AsyncTask上做一個get()。我希望他們刪除電話以阻止用戶使用它。 –

+0

您可能會收到NetworkOnMainThreadException。你不應該在主線程上運行網絡任務。使用ASyncTask。 您還應該彈出問題中的堆棧跟蹤,以便我們可以看到錯誤。 – apmartin1991

回答

1

@AnshaySaboo有Azure上的兩個服務命名Azure Mobile Services(Azure上的經典老門戶服務)& Azure Mobile Apps(Azure上的新門戶新服務)。根據您的描述,我無法識別您使用了哪種服務。但是,基於構造函數MobileServiceClient(String appUrl, Context context),我認爲您使用的是Azure Mobile Apps SDK for Android,而不是Azure Mobile Services SDK for Android

我認爲這個問題可能是由於在Azure新門戶上創建移動應用服務時,使用Mobile Service SDK for Android造成的。因此,請檢查您用作後端的服務以及您用於Android客戶端的sdk,並確保您的後端和客戶端的服務一致性。

作爲參考,有一些資源用於不同的服務。

  1. 開始使用Mobile Services vs Mobile Apps
  2. 如何使用Android客戶端庫Mobile Services vs Mobile Apps
  3. GitHub上的示例:Mobile Services vs Mobile Apps
相關問題