Adding a Progress while making a GET Request

1 min read

Dear Friends

As we have already made   Http Get Request Example Link

We will expand it and try to show a circular progress bar while making network request. So to start with we will add a progress bar in xml layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="in.relsellglobal.jsonparsingdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

    <ProgressBar
        android:id="@+id/pb"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/AlertDialog.AppCompat"
        android:indeterminate="true"
    />

</RelativeLayout>

we will  play with the visibility attribute of progress bar element at right place in our code.

Please have a closer look at Asynctask .

 

public class JSONParsingTask extends AsyncTask<Void,Void,String> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(Void... params) {

        String url = "http://relsellglobal.in/DeliveryS/CRKInsightsServer/mobilecheck.php?controlVar=32";
        StringBuffer stringBuffer = new StringBuffer();

        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

            int code = connection.getResponseCode();

            if(code == HttpURLConnection.HTTP_OK) {

                InputStream inputStream = connection.getInputStream();

                String line = "";


                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                while((line = bufferedReader.readLine()) != null) {
                    stringBuffer.append(line);
                }

                return stringBuffer.toString();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pb.setVisibility(View.GONE);

        if(s!=null) {
            try {
                JSONObject jsonObject = new JSONObject(s);
                JSONObject jsonObjectList = jsonObject.getJSONObject("list");
                JSONArray jsonArray = jsonObjectList.getJSONArray("data");

                for(int i=0;i<jsonArray.length();i++) {
                    JSONObject obj = (JSONObject) jsonArray.get(i);
                    WebPojo webPojo = new WebPojo();
                    webPojo.setId(obj.getString("ID"));
                    webPojo.setUsername(obj.getString("USERNAME"));
                    webPojo.setFcmId(obj.getString("FCMID"));
                    webPojoArrayList.add(webPojo);
                }


            } catch (JSONException e) {
                e.printStackTrace();
            }

            System.out.println(webPojoArrayList);


        }


    }
}

 

There a few callbacks methods called whenever a request  to execute asynctask is made.

After the constructor of Asynctask is called its onPreExecute method that gets called. Now in onPreExecute you can modify visibility of UI Element ( ProgressBar). So in UI you will see a progress loader on screen. Now doInBackground callback will be called and we actually hit http url to get our data. Once we get the data in onPostExecute we again make visibility of ProgressBar as gone.

 

This is just an example how progressbar is used.  We can definitely used it in more complex ways.