android - Volley 多个请求 :How to call more than 1 request in one Activity

标签 android android-volley

问题描述:

  • 我有四种方法可以从服务器获取相关事物的所有数据:
  • BankList(); BranchList(); StateDetails(); DistrictDetails();
  • 我正在使用 Volley 从服务器中一一获取与银行详细信息相关的数据。并存储到 SQLite 中。

  • 问题:
  • 请查看以下代码
  • 什么是一一调用 volley String Request 的正确方法。
  • 如何优先或自动调用 volley String 请求。
  • 代码运行良好,并将其值存储到 SQLite 中。
  • 但加载程序进度对话框无法正常工作。它卡住应用程序它不能正常工作。它给我输出但应用程序卡住几秒钟如果网络速度很慢,如 3G 然后花了更多时间,然后我的应用程序将卡住更多时间。所以请帮助我如何避免
  • 我的问题是我的方式是对还是错。如何一个接一个地调用方法。优先级或自动一个一个

  • 请查看以下代码
    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);
    
    
        }
    
    
    }
    

    最佳答案

    你的代码有问题。 Volley 抽射onResponse()onErrorResponse()方法总是在主 UI 线程中调用。在您的代码中,您将 SQLite 数据库写入操作放在 volley onResponse() 中。方法。数据库写入操作可能是一个长时间运行的过程,具体取决于数据的大小。

    在您的代码中,主 UI 线程休眠并等待数据库写入操作完成,这就是您的应用程序用户界面卡住的原因。将数据库写入操作放在单独的后台线程上将解决您的问题。

    使用 AsyncTask 用于写操作。

    关于android - Volley 多个请求 :How to call more than 1 request in one Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41634535/

    相关文章:

    android - 不可见 View 的触摸能力

    android - 显示按钮位于屏幕最底部和右侧

    iphone - 我可以通过浏览器和 JavaScript 以编程方式访问智能手机的传感器吗?

    安卓 : Is possible to add calendar event without calling the Google Calendar API or Starting Native Calendar activity?

    android - 使用 Android Volley 的 HTTPS 请求

    android - DefaultRetryPolicy中的backoffMultiplier是什么意思?

    android - 有时,Volley 无法从服务器返回响应

    android - Volley : BasicNetwork. performRequest:意外的响应代码 500

    未指定 Android SDK 路径

    android - 使用 Android volley 进行单元测试