Java - NullPointerException when getting JSONObject

前端 未结 1 1988
别跟我提以往
别跟我提以往 2021-01-22 09:38

I\'m trying to implement a login feature in my Android app using Lukencode\'s RestClient class. I have a class named UserFunctions to retrieve JSONObject via RestCl

相关标签:
1条回答
  • 2021-01-22 10:15

    I won't expand on how you should do this, but only on why it happens. An AsyncTask, by definition, is asynchronous. This means that when you execute it, it does its job in parallel, in a different thread. So you can't expect it to be terminated right after execute() has been called (else it would be called SyncTask, and wouldn't have any reason to exist).

    See AsyncTask as a toaster. When you start the toaster (execute the AsyncTask), it toasts your bread, but doesn't return the toasted bread immediately. It takes some time to toast it. While it's toasting your bread, you can do something else (and thus not freeze the UI thread completely). And when it has finished toasting your bread, it notifies you with a ding! that the bread is toasted. That's the same thing here. You should only start using json (the toasted bread), when the AsyncTask notifies you that it has finished executing.

    0 讨论(0)
提交回复
热议问题