0

I'm developing an Android App and I've created an App services in Azure & I've been trying to understand how to communicate with the DB through the MobileAppServices.

I was successful to Add items into the Tables (Easy tables) but, I couldn't retrieve data from the tables.

I've tried in 2 ways:

Retrieving data with/without AsyncTask.

(1) I get An error. (Using AsyncTask)

public void onClickVerify(View view)  {

        if (mClient == null)
            return;

        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    final List<Users> URS = GetItemsFromMobileServiceTable();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Verified = "Account is not Verified";
                            for (Users i : URS) {
                                if (i.getUN() == null)
                                    break;

                                else if( i.getUN() == Username.toString()) {
                                    Verified = "Account Verified";
                                }
                            }
                        }
                    });
                } catch (final Exception e) {}

                return null;
            }
        }.execute();

        runAsyncTask(task);

    }


   private List<Users> GetItemsFromMobileServiceTable() throws ExecutionException, InterruptedException, MobileServiceException {
        return mUser.where().field("UN").eq(Username.toString()).execute().get();
    }
                                              --------- beginning of crash 03-14 12:40:54.623 3500-3500/com.himk.karam.h

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.himk.karam.h, PID: 3500 java.lang.IllegalStateException: Could not execute method for android:onClick at android.view.View$DeclaredOnClickListener.onClick(View.java:4725) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.view.View$DeclaredOnClickListener.onClick(View.java:4720) at android.view.View.performClick(View.java:5637)  at android.view.View$PerformClick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Caused by: java.lang.IllegalStateException: Cannot execute task: the task is already running. at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:609) at com.himk.karam.h.Login.runAsyncTask(Login.java:183) at com.himk.karam.h.Login.onClickVerify(Login.java:114) at java.lang.reflect.Method.invoke(Native Method)  at android.view.View$DeclaredOnClickListener.onClick(View.java:4720)  at android.view.View.performClick(View.java:5637)  at android.view.View$PerformClick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

(2) when launching the Activity that contains that Retrieving action the Activity doesn't load.(without using AsynTask)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    try {
        mClient = new MobileServiceClient("https://khim.azurewebsites.net",this);

        mClient.setAndroidHttpClientFactory(new OkHttpClientFactory() {
            @Override
            public OkHttpClient createOkHttpClient() {
                OkHttpClient client = new OkHttpClient();
                client.setReadTimeout(20, TimeUnit.SECONDS);
                client.setWriteTimeout(20, TimeUnit.SECONDS);
                return client;
            }
        });

        List<Users> mUsers = mClient.getTable(Users.class).execute().get();//if I remove this line the activity will load normally       
        Username = (EditText) findViewById(R.id.etUN);
    } catch (MalformedURLException | InterruptedException | ExecutionException | MobileServiceLocalStoreException e) {
        e.printStackTrace();
    }
}

NOTE: ALL I WANT TO DO IS TO HAVE THE DATA IN A LIST IN ANDROID I DON'T WANT TO VIEW IT TO THE USER.

Karam G
  • 1
  • 2
  • `Caused by: java.lang.IllegalStateException: Cannot execute task: the task is already running.` As the exception itself explains, you cannot execute an **AsyncTask** more than once. You can check out [this similar issue](http://stackoverflow.com/questions/6879584/how-to-run-the-same-asynctask-more-than-once) on SO for more info. – Aaron Chen Mar 15 '17 at 08:40

1 Answers1

0

Like Aaron Chen points out, the exception is about executing AsyncTask more than once. In your #1 implementation using AsyncTask, the task is executed twice. First time in AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){@Override protected Void doInBackground(Void... params) { /*...*/ }.execute();, the second time in mUser.where().field("UN").eq(Username.toString()).execute().get();. If you remove the execute() from the first one, it should work just fine.

In your #2 implementation, the activity doesn't load because you're running an async query on UI thread in onCreate() method. If you run it on a background thread, this problem will go away. You can check out this azure documentation on how to query data from your Mobile App backend. There's plenty of examples that should be helpful.

Di Hei
  • 41
  • 7