php - 使用 php volley 库从服务器获取大量 json 数据花费太多时间

标签 php android mysql json android-volley

我正在开发一个使用 volley 库从 PHP 服务器同步数据的应用程序。我正在接收来自 php 的 json 数组响应。当响应很少时,它工作得很好。但是当 json 非常大时就会出现问题。我正在使用 xaamp 进行同步。加载数据花费了太多时间。有时根本得不到任何数据。我正在准备来自 json php 数组的语句,并将其添加到简单数组列表中。

我收到 logcat 错误:

36.713ms
W/art: Suspending all threads took: 7.657ms
I/art: Waiting for a blocking GC Alloc
W/art: Suspending all threads took: 85.126ms
W/art: Suspending all threads took: 40.203ms
I/art: Background partial concurrent mark sweep GC freed 541(18KB) AllocSpace objects, 33(130MB) LOS objects, 11% free, 123MB/139MB, paused 42.528ms total 557.827ms
I/art: WaitForGcToComplete blocked for 328.549ms for cause Alloc
I/art: Starting a blocking GC Alloc
W/art: Suspending all threads took: 5.369ms
I/art: Background sticky concurrent mark sweep GC freed 105(3KB) AllocSpace objects, 3(11MB) LOS objects, 0% free, 142MB/142MB, paused 7.807ms total 52.573ms
W/art: Suspending all threads took: 5.261ms
W/art: Suspending all threads took: 294.041ms
W/art: Suspending all threads took: 38.308ms
I/art: Background partial concurrent mark sweep GC freed 388(13KB) AllocSpace objects, 26(103MB) LOS objects, 10% free, 133MB/149MB, paused 40.009ms total 515.759ms
W/art: Suspending all threads took: 5.063ms
I/art: Background sticky concurrent mark sweep GC freed 157(5KB) AllocSpace objects, 3(11MB) LOS objects, 0% free, 150MB/150MB, paused 7.052ms total 34.742ms
I/art: Waiting for a blocking GC Alloc
W/art: Suspending all threads took: 129.556ms
I/art: Background partial concurrent mark sweep GC freed 494(16KB) AllocSpace objects, 36(142MB) LOS objects, 12% free, 110MB/126MB, paused 1.517ms total 672.880ms
I/art: WaitForGcToComplete blocked for 485.833ms for cause Alloc
I/art: Starting a blocking GC Alloc
W/art: Suspending all threads took: 13.120ms
I/art: Background sticky concurrent mark sweep GC freed 104(3KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 138MB/138MB, paused 16.321ms total 38.525ms
W/art: Suspending all threads took: 9.983ms
W/art: Suspending all threads took: 285.230ms
W/art: Suspending all threads took: 38.702ms
I/art: Background partial concurrent mark sweep GC freed 435(14KB) AllocSpace objects, 27(106MB) LOS objects, 11% free, 127MB/143MB, paused 40.305ms total 493.740ms
W/art: Suspending all threads took: 5.107ms
I/art: Background sticky concurrent mark sweep GC freed 156(5KB) AllocSpace objects, 3(11MB) LOS objects, 0% free, 145MB/145MB, paused 6.856ms total 25.414ms
W/art: Suspending all threads took: 413.504ms
W/art: Suspending all threads took: 56.440ms
I/art: Background partial concurrent mark sweep GC freed 14(464B) AllocSpace objects, 9(35MB) LOS objects, 12% free, 115MB/131MB, paused 59.022ms total 496.519ms
I/art: Background sticky concurrent mark sweep GC freed 154(5KB) AllocSpace objects, 3(11MB) LOS objects, 0% free, 133MB/133MB, paused 5.741ms total 23.296ms
I/art: Waiting for a blocking GC Alloc
W/art: Suspending all threads took: 96.541ms
W/art: Suspending all threads took: 43.589ms
I/art: Background partial concurrent mark sweep GC freed 548(18KB) AllocSpace objects, 33(130MB) LOS objects, 11% free, 122MB/138MB, paused 45.755ms total 511.247ms
I/art: WaitForGcToComplete blocked for 332.277ms for cause Alloc
I/art: Starting a blocking GC Alloc
W/art: Suspending all threads took: 12.093ms
I/art: Background sticky concurrent mark sweep GC freed 104(3KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 150MB/150MB, paused 15.972ms total 51.520ms
W/art: Suspending all threads took: 7.796ms

我的 json 数组如下链接所示。 http://www.ktronsystem.com/data.json

我的安卓代码是

        public void m461async(){
        if (!(co.loggedInUserUniqid == "")) {
            jdialog= new ProgressDialog(all.this);
            jdialog.setMessage("m462   Please wait..We are processing your request.");
            jdialog.setIndeterminate(false);
            jdialog.setCancelable(false);
            jdialog.show();

            final Cursor mall = co.data_db.rawQuery("select * from mall", null);
            mall.moveToFirst();
            final int mrows = mall.getCount();
            System.out.print(mrows+"    ababababababababqa" );

            for (int idx = 0; idx < mrows; idx++) {
                System.out.print("loop of m462 55555555555555555555555");
                mall.moveToPosition(idx);
                mtempidx = idx;
                final String mfilename = f1.getCol(mall, "filename").toLowerCase().trim();
                String mfilepath = co.getDataPath(cn) + "/";
                final String mfilefullname = mfilepath + mfilename;

                if (new File(mfilefullname).exists()) {
                    StringRequest stringRequest= new StringRequest(Request.Method.POST, murl + "getonlinem461data.php",
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(final String response) {
                                    mjay = mjay + 1;
                                    System.out.println(mjay+ "  m461111111111111111"+response);

                                    class m461prepare extends AsyncTask<String, String, Integer> {
                                        @Override
                                        protected Integer doInBackground(String... params) {
                                            msuccess=1;
                                            System.out.println(mjay+"  m46211111111111111"+response);
                                            final String mquery="insert into m462 (accode,book,date,vno1,vno2,vno3,part,tot_debit,tot_credit,balance,cl_type,m462index) values ";
                                            String mquery2="";

                                            try {
                                                System.out.print("zzzzzzzzzzzzzzzzzzzzzzzzzzz");

                                                final SQLiteDatabase mdb1 =SQLiteDatabase.openDatabase(co.getDataPath(cn)+"/"+mfilename, null, SQLiteDatabase.OPEN_READWRITE);
                                                mdb1.execSQL("delete from m462");
                                                mdb1.close();
                                                JSONArray arr = new JSONArray(response);
                                                final ContentValues insertValues = new ContentValues();
                                                System.out.print("xxxxxxxxxxxxxxxxxxxxxx");

                                                if (arr.isNull(0)==false) {
                                                    System.out.print("ccccccccccccccccccccccccc");

                                                    for (int i = 0; i < arr.length(); i++) {
                                                        final JSONObject obj = (JSONObject) arr.get(i);
                                                        final String maccode = obj.get("maccode").toString();
                                                        final String mbook = obj.get("mbook").toString();
                                                        final String mdate = obj.get("mdate").toString();
                                                        final String mvno1 = obj.get("mvno1").toString();
                                                        final String mvno2 = obj.get("mvno2").toString();
                                                        final String mvno3 = obj.get("mvno3").toString();
                                                        final String mpart = obj.get("mpart").toString();
                                                        final String mtot_debit = obj.get("mtot_debit").toString();
                                                        final String mtot_credit = obj.get("mtot_credit").toString();
                                                        final String mbalance = obj.get("mbalance").toString();
                                                        final String mcl_type = obj.get("mcl_type").toString();
                                                        final String mm462index = obj.get("mm462index").toString();

                                                        mquery2 = mquery2
                                                                + "('" + f1.abc(maccode) + "'," +
                                                                "'" + mbook + "'," +
                                                                "'" + mdate + "'," +
                                                                "'" + mvno1 + "'," +
                                                                "'" + mvno2 + "'," +
                                                                "'" + mvno3 + "'," +
                                                                "'" + mpart + "'," +
                                                                "'" + mtot_debit + "'," +
                                                                "'" + mtot_credit + "'," +
                                                                "'" + mbalance + "'," +
                                                                "'" + mcl_type + "'," +
                                                                "'" + mm462index + "'),";
                                                    }
                                                    System.out.print("vvvvvvvvvvvvvvvvvvvvvvv");

                                                    String mfinalquery = (mquery.trim() + mquery2.trim());
                                                    mfinalquery = mfinalquery.substring(0, mfinalquery.length() - 1);

                                                    ArrayQuery.add(mfinalquery);
                                                    ArrayFilename.add(mfilefullname);
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            System.out.print("bbbbbbbbbbbbbbbbbbbbbbb");

                                            return 0;
                                        }
                                        @Override
                                        protected void onPostExecute(Integer a){
                                            System.out.print("nnnnnnnnnnnnnnnnnnnnnnnnnnnn");

                                            if (mall.isLast()) {
                                                System.out.println("333333333333");
                                                jdialog.dismiss();
//                                                trasync();
                                            }
                                        }
                                    }

                                    new m461prepare().execute();
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    merror=1;
                                    jdialog.dismiss();
                                    f1.wait_wind("Please check your internet connection", cn);
                                }
                            }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params = new HashMap<>();
                            params.put("uniqid_guid", co.loggedInUserUniqid);
                            params.put("filename", mfilename);
                            return params;
                        }
                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(999999999, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                    AppController.getInstance().addToRequestQueue(stringRequest,"m462_req");

                }
            }
            mall.close();
        }
        else{
            f1.wait_wind("Please login first !",cn);
        }
    }

提前致谢。

最佳答案

如果文件的长度很大,你应该在后台运行这个方法。试试这段代码:

 @Override
public void onResponse(String  response) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            JSONArray arr = new JSONArray(response);

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

               //write your parsing logic here



            }

        }
    }).start();
}

关于php - 使用 php volley 库从服务器获取大量 json 数据花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41520188/

相关文章:

php - 向 mysql 数据库添加数据

php - 在php中注销和重定向 session

php - Varnish ESI和Wordpress-模拟加载帖子

android - 收到 GCM 消息时如何启动应用程序而不是构建通知?

php - 从一个php脚本获取变量到另一个php脚本

mysql - INSERT ... ON DUPLICATE KEY UPDATE 在我的数据库中不起作用

php - 如何获取远程存储图像的文件大小? (php)

php - array_shift 来自数组中的数组

Android:如何使用 CursorAdapter 将 child 添加到我的自定义 ViewGroup?

android - 打开添加联系人 Activity android sdk 2.1 及以上版本