Json load dynamic to Run Run Time

I have request json from server and handle with Handler duration 50000. sometime it loaded all json sometime it not yet loaded. What i want is Run Handler dynamic to the Json load. if All json loaded I want the run duration equal to 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() {
                    @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 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 params = new HashMap();
                        params.put("api_key", "[email protected]");
                        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);="" }
  

Try this,

// 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() {
            @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);
}
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Json load dynamic to Run Run Time

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录