【问题标题】:Volley Multiple Request :How to call more than 1 request in one ActivityVolley Multiple Request:如何在一个活动中调用多个请求
【发布时间】:2017-01-13 12:20:09
【问题描述】:

问题说明:

  1. 我有四种方法可以从服务器获取所有相关事物的数据:

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

  1. 我正在使用 Volley 从服务器中一一获取与银行详细信息相关的数据。并存储到 SQLite 中。

问题:

  1. 请检查以下代码

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

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

  4. 代码运行良好,并将其值存储到 SQLite 中。

  5. 但加载程序进度对话框无法正常工作。它冻结应用程序它无法正常工作。它给我输出但应用程序冻结几秒钟如果网络速度像 3G 这样很慢然后它需要更多时间然后我的应用程序将冻结更多时间。所以请帮助我如何避免这种情况

  6. 我的问题是我的方式是对还是错。如何一个接一个地调用方法。优先级或自动一个接一个

请检查以下代码

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


    }


}

【问题讨论】:

  • 使用接口回调。
  • @DheerubhaiBansal 感谢评论。你能给我举个例子吗?
  • 查看此接口示例 - 从那里您可以从同一活动中触发任意数量的请求:stackoverflow.com/questions/28172496/…

标签: android android-volley


【解决方案1】:

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

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

使用 AsyncTask 进行写入操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多