开发者

How to display the UI with the updated data from the server in android

I am developing one application which will send the request to server and get the response with data and then launch the homepage with the updated data.

I am using following approach:

  1. create handler in UI side and subscribe with the service.
  2. after subscribing with the service, send request to the server via service.
  3. service received the data and update the data in the registered handler.

Problem: My observation is my homepage is getting launched with the default value so it seems like before getting subscribe with the service ,Ui is getting launched.

I want to restrict homepage display with default value, when response come from server with the开发者_开发技巧 data then only it should display with the updated data. OR it should display busy dialog until homepage gets updated with the received data, like how it behaves when we click on to get the weather/stock information on android phone.

So anyone could help with the appropriate approach? If someone can share with the example then it would be great for me.

Thanks, Piks


Just go through the AsyncTask, its easy to implement Threading in android, also known as Painless Threading.

Let me explore more about AsyncTask. It provides 4 methods:

  1. doInBackground() - Perform background task here
  2. onPostExecute() - Perform Display result type of operations
  3. onProgressUpdate() - Perform update on UI based on the progress update
  4. onPreExecute() - Perform all the task which are needed before the execution of doInBackground() process, mostly we display ProgressBar(Busy dialog with Please Wait... text).

For example:

private class LongOperation extends AsyncTask {

    @Override
    protected void onPreExecute() {
       // Things to be done before execution of long running operation. For example showing ProgessDialog
    }

    @Override
    protected String doInBackground(String... params) {
       // perform long running operation operation
      return null;
    }

    @Override
    protected void onPostExecute(String result) {
      // execution of result of Long time consuming operation
    }

    @Override
    protected void onProgressUpdate(Void... values) {
      // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
    }
}

Now you can execute AsyncTask as:

new LongOperation().execute(params);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜