Json将动态加载到Run handler Duration

时间:2016-11-21 03:42:48

标签: android json

我已从服务器请求json并处理Handler持续时间50000。 有时它加载所有json,有时还没有加载。 我想要的是Run Handler动态到Json负载。如果加载了所有json,我希望运行持续时间等于0。

public void onLoadMore() {
        Log.d("MainActivity_","onLoadMore");
        mAdapter.setProgressMore(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                itemList.clear();
                mAdapter.setProgressMore(false);
                int start = mAdapter.getItemCount();
                final int end = start + 5;


                RequestQueue queue = Volley.newRequestQueue(context);
                HttpsTrustManager.allowAllSSL();
                final String url = "https://www.iknow.com.kh/api/business/get_business_home_latest.php";

                StringRequest stringRequest = new StringRequest(context, Request.Method.GET,url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("JsonBusiness ", response);
                        JSONObject business;
                        JSONObject data;

                        JSONArray operator;
                        try {
                            data = new JSONObject(response);
                            business  = data.getJSONObject("business");
                            operator = business.getJSONArray("content");
                            for (int i = 0; i < operator.length(); i++) {
                                JSONObject each_report = new JSONObject(operator.get(i).toString());
                                Log.d("cat _ ID:", each_report.getString("business_name"));
                                String desc, phone, subaddress, category, businessname;

                                if(each_report.getString("business_name").length()>25){
                                    businessname=each_report.getString("business_name").substring(0,25)+"...";
                                }else {
                                    businessname=each_report.getString("business_name");
                                }

                                if(each_report.getString("description").length()>35){
                                    desc=each_report.getString("description").substring(0,35)+"...";
                                }else
                                {
                                    desc=each_report.getString("description")+"...";
                                }

                                if(each_report.getString("phone").length()>35){
                                    phone=each_report.getString("phone").substring(0, 35)+"...";
                                }else {
                                    phone=each_report.getString("phone");
                                }

                                String address=each_report.getString("house") + ", " + each_report.getString("street") + ", " + each_report.getString("pro");
                                if(address.length()>35){
                                    subaddress=address.substring(0,35)+"...";
                                }else {
                                    subaddress=address;
                                }
                                category="Category: " + each_report.getString("cate_name");

                                itemList.add(new BusinessEntity(each_report.getString("first_letter"), businessname, desc ,phone,subaddress,category,each_report.getString("bussID"),each_report.getString("CID"),each_report.getString("PID")));
//                        bcontractor.add(b_list);
                            }
                            mAdapter.addAll(itemList);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(context, "Login Error", Toast.LENGTH_SHORT).show();
                    }
                }){
                    @Override
                    public String getUrl() {
                        Map<String, String> params = new HashMap<>();
                        params.put("api_key", "iknow@ApIKeY");
                        params.put("search_letter", "");
                        params.put("offset", ""+end);
                        params.put("limit", "15");

                        Log.d("Url with Param___", SetUrl(url, params));
                        return SetUrl(url, params);
                    }
                };
                queue.add(stringRequest);


//                for (int i = start + 1; i <= end; i++) {
//                    itemList.add(new BusinessEntity("F","Item " + i,"","","","","","",""));
//                }
                mAdapter.addItemMore(itemList);
                mAdapter.setMoreLoading(false);
            }
        },50000);
    }

2 个答案:

答案 0 :(得分:0)

试试这个,

// Create handler
Handler mHandler = new Handler();

// Create Runnable task
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        ...
        StringRequest stringRequest = new StringRequest(context, Request.Method.GET,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                ...
                mAdapter.notifyDataSetChanged();

                // data has been loaded, call onLoadMore again
                onLoadMore();
            }

        });
        queue.add(stringRequest);
        ...
    }
};

public void onLoadMore() {
    Log.d("MainActivity_","onLoadMore");
    mAdapter.setProgressMore(true);

    // Reducing the time to 2 seconds. Just an arbitrary value
    mHandler.postDelayed(runnable, 2000); 
}

答案 1 :(得分:0)

第一次,您可以立即运行您的任务,并在加载数据后,您可以设置另一个适当的延迟,以便在一段时间后运行它。请参阅以下代码

// Create handler
Handler mHandler = new Handler();

// Create Runnable task
Runnable runnable = new Runnable() {
    @Override
    public void run() {
      ...
            }

        });
        queue.add(stringRequest);

        mAdapter.addItemMore(itemList);
        mAdapter.setMoreLoading(false);
        mHandler.postDelayed(runnable, 4000); // now register to run after 4 secs 
    }
};

public void onLoadMore() {
    Log.d("MainActivity_","onLoadMore");
    mAdapter.setProgressMore(true);

    // Run immediately at the start
    mHandler.post(runnable); 
}