Home Development for Android Making ProgressBar spin while an http request is going on

Making ProgressBar spin while an http request is going on

by admin

It all started when I suddenly felt like making something for android.All this despite the fact that java is not my native language.
While making my app I faced some difficulties and I want to tell you about their solutions. I think they will be useful for newbies like me and I will be thankful for tips and more beautiful examples from gurus.
But the main proof of the examples working is that the application works, you can check
Here are a few of the problems I encountered :

  • UI waits for response from server, how to defeat? (in this article)
  • Sending and receiving data – in what format? (in the following one, if anyone is interested)
  • How to work the camera? (also in the next one, if anyone is interested)

Consider the first problem for now.

UI is waiting for a response from the server, how do I defeat it?

Interacting with the server is the key point of this application. When the application sends a request to the server, it has to wait for a response. During this wait, the user interface stops and the program seems to freeze. It’s not a problem if the response is received within a second, but in reality, it sometimes takes about 10 seconds to get a response from the server. To tell the user that the application is not hung, you need to show them the ProgressBar. I settled on a spinning wheel, as in AJAX. Here is what it looks like :
Making ProgressBar spin while an http request is going on
Here is a rough description of how it works. I have some class HttpClient, it is responsible for interaction with the server. It also starts and stops ProgressBar. This class works asynchronously. That is the application that starts it does not wait until it works out, and can continue its work. It is achieved by the following construction :

//declare a classpublic class HttpClient extends AsyncTask<Void, Integer, Long> {...//declare dialogpublic ProgressDialog dialog;// context of the parent classContext ctx;...// start ProgressBar at the moment of thread startprotected void onPreExecute() {dialog = new ProgressDialog(ctx);dialog.setMessage("Search...");dialog.setIndeterminate(true);dialog.setCancelable(true);dialog.show();}// the thread itself, SendHttpPost() is our long running methodprotected Long doInBackground(Void... params) {try {response = SendHttpPost();} catch (Exception e) {e.printStackTrace();response = null;}return null;}// as soon as we receive a response from the server, we switch ProgressBar offprotected void onPostExecute(Long unused) {dialog.dismiss();...// here we can tell the parent class that we're done// I'm doing it a little crooked:((Runnable)ctx).run();....super.onPostExecute(unused);}....}

The parent class that calls HttpClient looks something like this :

public class SearchableActivity extends ListActivity implements Runnable {private HttpClient req;...// this method is called when it is necessary to receive data from the serverprivate void showResults(String query) {...req = new HttpClient();req.execute((Void)null);}// this method is called from the HttpClient, when the response from the server is received and you can do something with itpublic void run() {...// handle req in whatever way we want and, if necessary, redraw the Activity...}}

That I went the Runnable route seems very controversial to me, but I had no other thoughts at the time.
I learned how to load and display pictures asynchronously from this article: habrahabr.ru/blogs/android/78747 Take a moment to say thank you rude !

You may also like