Yogesh Borhade
Yogesh Borhade

Reputation: 704

Volley Multiple Request :How to call more than 1 request in one Activity

Questions Description:

  1. I Have four methode to get all the data of related things from server:

BankList(); BranchList(); StateDetails(); DistrictDetails();

  1. In that I am fetching data From Server related with Bank Details one by one using Volley .and Storing into SQLite.

Quetions:

  1. Please Check below Code

  2. What is Correct way to call volley String Request one by one.

  3. How to call volley String request one by One on Priority or automatically.

  4. Code is Working Fine and its storing value into SQLite Also.

  5. But Loader Progress Dialog not working Properly . its freezing the app its not working properly .it gives me output but application freeze for few second if network speed is slow like 3G then it took more time and then my application will freeze for more time . so please help me how to avoid that

  6. My Question is My way is correct or wrong. how to call methods one after another . on priority basis or automatically one by one

Please Check below Code

public class LoginPage extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);


        BankList();
        BranchList();
        StateDetails();
        DistrictDetails();



   }

    public void BankList() {

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Fetching Data");
        pDialog.setCancelable(false);
        pDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                BankURL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        pDialog.hide();
                        result = response;
                        Log.e("Responce is ", result);

                        File dbtest = new File(Utils.DB_PATH + Utils.DB_NAME);

                        if (dbtest.exists()) {

                            Log.e("db create", "Databse is Created");

                            String myPath = DB_PATH + DB_NAME;
                            SQLiteDatabase db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                            db.delete("BankList", null, null);

                            try {

                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int BankID = jsonObject.getInt("BankID");
                                    String BankName = jsonObject.getString("BankName");
                                    Log.e("Bank ID", String.valueOf(BankID));
                                    Log.e("Bank Name", BankName);


                                    ContentValues cv = new ContentValues();
                                    cv.put("BankID", BankID);
                                    cv.put("BankName", BankName);
                                    db.insert("BankList", null, cv);


                                }
                            } catch (Exception e) {

                            }
                        } else {
                            Log.e("db Not created ", "Just Inserted Value");

                            SQLDatabase so = new SQLDatabase(getApplicationContext(), DB_NAME, null, 1);

                            try {
                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int BankID = jsonObject.getInt("BankID");
                                    String BankName = jsonObject.getString("BankName");
                                    Log.e("Bank ID", String.valueOf(BankID));
                                    Log.e("Bank Name", BankName);
                                    so.BankList(BankID, BankName);

                                }
                            } catch (Exception e) {

                            }

                        }


                    }
                }

                , new Response.ErrorListener()

        {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                pDialog.hide();

            }
        }

        )

        {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("getdata", "BankList");

                return params;
            }

        };

// Adding request to request queue
        VolleyAppController.getInstance().

                addToRequestQueue(stringRequest);


    }


    public void StateDetails() {

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Fetching Data");
        pDialog.setCancelable(false);
        pDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                BankURL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        pDialog.hide();
                        result = response;
                        Log.e("Responce is ", result);

                        File dbtest = new File(Utils.DB_PATH + Utils.DB_NAME);

                        if (dbtest.exists()) {

                            Log.e("db create", "Databse is Created");

                            String myPath = DB_PATH + DB_NAME;
                            SQLiteDatabase db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                            db.delete("StateList", null, null);

                            try {

                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int StateID = jsonObject.getInt("StateID");
                                    String StateName = jsonObject.getString("StateName");
                                    int Bank_id = jsonObject.getInt("Bank_id");


                                    ContentValues cv = new ContentValues();
                                    cv.put("StateID", StateID);
                                    cv.put("StateName", StateName);
                                    cv.put("Bank_id", Bank_id);

                                    db.insert("StateList", null, cv);


                                }
                            } catch (Exception e) {

                            }
                        } else {
                            Log.e("db Not created ", "Just Inserted Value");

                            SQLDatabase so = new SQLDatabase(getApplicationContext(), DB_NAME, null, 1);

                            try {
                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int stateId = jsonObject.getInt("StateID");
                                    String stateName = jsonObject.getString("StateName");
                                    int bankID = jsonObject.getInt("Bank_id");

                                    so.StateList(stateId, stateName, bankID);

                                }
                            } catch (Exception e) {

                            }

                        }


                    }
                }

                , new Response.ErrorListener()

        {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                pDialog.hide();

            }
        }

        )

        {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("getdata", "StateList");

                return params;
            }

        };

// Adding request to request queue
        VolleyAppController.getInstance().

                addToRequestQueue(stringRequest);


    }

    public void DistrictDetails() {

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Fetching Data");
        pDialog.setCancelable(false);
        pDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                BankURL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        pDialog.hide();
                        result = response;
                        Log.e("Responce is ", result);

                        File dbtest = new File(Utils.DB_PATH + Utils.DB_NAME);

                        if (dbtest.exists()) {

                            Log.e("db create", "Databse is Created");

                            String myPath = DB_PATH + DB_NAME;
                            SQLiteDatabase db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                            db.delete("DistrictList", null, null);

                            try {

                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int DistrictId = jsonObject.getInt("DistrictId");
                                    String DistrictName = jsonObject.getString("DistrictName");
                                    int StateID = jsonObject.getInt("StateID");


                                    ContentValues cv = new ContentValues();
                                    cv.put("DistrictId", DistrictId);
                                    cv.put("DistrictName", DistrictName);
                                    cv.put("StateID", StateID);

                                    db.insert("DistrictList", null, cv);

                                }
                            } catch (Exception e) {

                            }
                        } else {
                            Log.e("db Not created ", "Just Inserted Value");

                            SQLDatabase so = new SQLDatabase(getApplicationContext(), DB_NAME, null, 1);

                            try {
                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int DistrictId = jsonObject.getInt("DistrictId");
                                    String DistrictName = jsonObject.getString("DistrictName");
                                    int StateID = jsonObject.getInt("StateID");

                                    so.DistrictList(DistrictId, DistrictName, StateID);

                                }
                            } catch (Exception e) {

                            }

                        }


                    }
                }

                , new Response.ErrorListener()

        {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                pDialog.hide();

            }
        }

        )

        {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("getdata", "DistrictList");

                return params;
            }

        };

// Adding request to request queue
        VolleyAppController.getInstance().

                addToRequestQueue(stringRequest);


    }

    public void BranchList() {

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Fetching Data");
        pDialog.setCancelable(false);
        pDialog.show();

        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                BankURL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        pDialog.hide();
                        result = response;
                        Log.e("Responce is ", result);

                        File dbtest = new File(Utils.DB_PATH + Utils.DB_NAME);

                        if (dbtest.exists()) {

                            Log.e("db create", "Databse is Created");

                            String myPath = DB_PATH + DB_NAME;
                            SQLiteDatabase db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
                            db.delete("BranchList", null, null);

                            try {

                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int BranchID = jsonObject.getInt("BranchID");
                                    String BranchName = jsonObject.getString("BranchName");
                                    String IFSC_code = jsonObject.getString("IFSC_code");
                                    int District_id = jsonObject.getInt("District_id");


                                    ContentValues cv = new ContentValues();
                                    cv.put("BranchID", BranchID);
                                    cv.put("BranchName", BranchName);
                                    cv.put("IFSC_code", IFSC_code);
                                    cv.put("District_id", District_id);

                                    db.insert("BranchList", null, cv);


                                }
                            } catch (Exception e) {

                            }
                        } else {
                            Log.e("db Not created ", "Just Inserted Value");

                            SQLDatabase so = new SQLDatabase(getApplicationContext(), DB_NAME, null, 1);

                            try {
                                JSONArray jsonArray = new JSONArray(response);

                                for (int i = 0; i <= jsonArray.length(); i++) {

                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    int BranchID = jsonObject.getInt("BranchID");
                                    String BranchName = jsonObject.getString("BranchName");
                                    String IFSC_code = jsonObject.getString("IFSC_code");
                                    int District_id = jsonObject.getInt("District_id");

                                    so.BranchList(BranchID, BranchName, IFSC_code, District_id);

                                }
                            } catch (Exception e) {

                            }

                        }


                    }
                }

                , new Response.ErrorListener()

        {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                pDialog.hide();

            }
        }

        )

        {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("getdata", "BranchList");

                return params;
            }

        };

// Adding request to request queue
        VolleyAppController.getInstance().addToRequestQueue(stringRequest);


    }


}

Upvotes: 4

Views: 4766

Answers (1)

prabeesh r k
prabeesh r k

Reputation: 31

There is problem with your code. The volley onResponse() and onErrorResponse() methods are always invoked in the main UI thread. In your code you place the SQLite database write operation inside the volley onResponse() method. The database write operation may be a long running process depending on the size of the data.

In your code the main UI thread sleep and wait for the database write operation to finish, and this is why your application user interface freeze. Place the database write operation on a separate background thread will solve your problem.

Use AsyncTask for the write operation.

Upvotes: 3

Related Questions