public class JsonServer extends AsyncTask<Void,Void,JSONObject> { private Context context; private ProgressDialog progressDialog; private String str; public JsonServer(Context context,String str){ this.context=context; this.str=str; } @Override protected void onPreExecute() { super.onPreExecute(); progressDialog=new ProgressDialog(context); progressDialog.setTitle("Получение данных"); progressDialog.show(); } @Override protected JSONObject doInBackground(Void... params) { try { Thread.sleep(2500); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(JSONObject jsonObject) { super.onPostExecute(jsonObject); progressDialog.dismiss(); } } 

How to call get from AsyncTask to make ProgressDialog work?

    1 answer 1

    In general, something like this:

     final JsonServer jsonServer=new JsonServer(getActivity(),"http://i/?group_id=15704"); jsonServer.execute(); new Thread(new Runnable() { @Override public void run() { try { jsonServer.get(); Log.e("asd","DA"); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); 

    Of course, this can be a bad code. If someone has a more correct code, please write.

    • one
      and what is the goal at all? Using AsyncTask imposes many restrictions, if you rotate the screen, asycktask at best returns data to a new activation, at worst data is lost. There is an excellent robospice library, which allows you to remove all these pitfalls, and write quality restfull applications - Dennis Zinkovsky
    • @DenisZinkovskiy, The goal is simple. Here we say in VK. When you move from top to bottom. then the lines go and the data in this window is updated. Since I can not do this yet. In general, it is necessary that it starts ProgressDialog, it is spinning until the data from the server is taken, and after that ProgressDialog turns off. Normal Download Window. Like games, etc. - Andro
    • it is very easy to do, there are many tutorials and libraries on the Internet that implement this feature, here’s one detailed article - Dennis Zinkovsky