MAUI SQLLite App not working on Android (apk)

Anonymous
2023-11-29T19:17:33.12+00:00

The below quiz app is working in Windows but not in Android , I tested on my Android phone using apk file.

It stopped working after I added sqllite , please suggest on this.

I used the version sqlite-net-pcl 1.6.292

     private async Task LoadQuestionsAsync()
        {




            List<InQuestion> res = await GetApiData();

            Questions.AddRange(res.Select(r => new Question
            {
                QuestionTitle = r.QuestionTitle,
                Options = new List<string>() { r.Opt1, r.Opt2, r.Opt3, r.Opt4 },
                Answer = r.Answer
            }));

        }
      async Task<List<InQuestion>> GetApiData()
        {
            string apiUrl = "https://sheet2api.com/v1/wlS0h0USxm9p/quiz";
            List<InQuestion> Questions = new List<InQuestion>();
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    // Make a GET request to the API
                    HttpResponseMessage response = await client.GetAsync(apiUrl);

                    // Check if the request was successful (status code 200)
                    if (response.IsSuccessStatusCode)
                    {
                        // Read the content of the response
                        string jsonString = await response.Content.ReadAsStringAsync();


                        // Deserialize the JSON string into an object
                        Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonString);
                        // truncate sqlLIte table Load sqlite table  from  Questions
                        //If the first time when you load the api data, it will push data to the sqlite DB.
                          List<InQuestion> itemInTheDB = await questionsDatabase.GetItemsAsync();
                        if (itemInTheDB.Count == 0)
                     {
                          foreach (InQuestion item in  Questions)
                           {
                             await questionsDatabase.SaveItemAsync(item);
                           }
                      }  

                    }
                    else
                    { // Load  Questions from sql lite  table
                         Questions = await questionsDatabase.GetItemsAsync();

                    }
                }
                catch (Exception ex)
                {
                    Questions = await questionsDatabase.GetItemsAsync();
                }
            }
            return Questions;
        }
 protected override Task OnInitializedAsync()
        {
           questionsDatabase = new QuestionsDatabase();
            LoadQuestionsAsync();

            return base.OnInitializedAsync();
        }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
 
namespace MauiAppMCQs.Models
{
    public class QuestionsDatabase
    {
        SQLiteAsyncConnection Database;
        private SQLiteAsyncConnection _dbConnection;
        public const string DatabaseFilename = "MCQs.db3";
        async Task Init()
        {  
        string dbPath  =
         Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
            _dbConnection = new SQLiteAsyncConnection(dbPath);
            if (Database is not null)
                return;
            Database = new SQLiteAsyncConnection(dbPath, Constants.Flags);
            var result = await Database.CreateTableAsync<InQuestion>();
        }
        public async Task<List<InQuestion>> GetItemsAsync()
        {
            await Init();
            var res = await Database.Table<InQuestion>().ToListAsync();
            return res;
        }
        public async Task<InQuestion> GetItemAsync(int id)
        {
            await Init();
            return await Database.Table<InQuestion>().Where(i => i.No == id).FirstOrDefaultAsync();
        }


        public async Task<int> SaveItemAsync(InQuestion item)
        {
            await Init();
            InQuestion res = await GetItemAsync(item.No);
            if (res != null)
                return await Database.UpdateAsync(item);
            else
                return await Database.InsertAsync(item);
        }


        public async Task<int> DeleteItemAsync(InQuestion item)
        {
            await Init();
            return await Database.DeleteAsync(item);
        }
    }
}


.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,190 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,836 Reputation points Microsoft Vendor
    2023-11-30T08:12:21.4733333+00:00

    Hello,

    I cloned your demo and you want to generate apk file.

    Firstly, right-click your project, choose the properties. Then click the Android tab, choose Manifest tab, change bundle to apk for Release & net7.0-android selection, then change the debug mode to release.

    After that, click the build tag and build your project.

    Next, open CMD and cd to your project's path, run dotnet publish -f:net7.0-android -c:Release /p:AndroidSigningKeyPass=xxx /p:AndroidSigningStorePass=xxx.

    As note: the generated apk is in the publish folder such as C:\Users\yourname\..\MauiAppMCQs-master\MauiAppMCQs-master\MauiAppMCQs\bin\Release\net7.0-android\publish.

    You need to choose com.companyname.mauiappmcqs-Signed.apk to install in your android emulator or device.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


0 additional answers

Sort by: Most helpful