排球多重请求:如何在一个活动中调用多个请求

时间:2017-01-13 12:20:09

标签: android android-volley

问题说明:

  1. 我有四种方法可以从服务器获取相关内容的所有数据:
  2. BankList(); BranchList(); StateDetails(); DistrictDetails();

    1. 因为我使用Volley一个接一个地从与银行详细信息相关的服务器获取数据并存储到SQLite中。
    2. Quetions:

      1. 请检查以下代码

      2. 什么是逐个调用volley String Request的正确方法。

      3. 如何在优先级或自动上逐个调用volley String请求。

      4. 代码工作正常,并将其存储在SQLite中。

      5. 但Loader Progress Dialog无法正常工作。它冻结了应用程序它无法正常工作。如果网络速度很慢,如 3G ,它会给我输出但应用程序冻结几秒钟然后花费更多时间然后我的应用程序将冻结更长时间。所以请帮助我如何避免

      6. 我的问题是我的方式是正确还是错误。如何一个接一个地调用方法。优先考虑或自动逐一

      7. 请检查以下代码

        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);
        
        
            }
        
        
        }
        

1 个答案:

答案 0 :(得分:1)

您的代码存在问题。始终在主UI线程中调用齐射onResponse()onErrorResponse()方法。在代码中,将SQLite数据库写操作放在volley onResponse()方法中。数据库写操作可以是长时间运行的过程,这取决于数据的大小。

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

使用 AsyncTask 进行写入操作。