0

Currently, I'm Working on the Running project, there i got a problem, The problem is JSON data is not loaded in the lower version of android. now i test on two devices, the code is working good on Android 10 but it calls the onFailure method in android lollipop.

I checked this documentation https://loopj.com/android-async-http/ here they told AsyncHttpClient is Compatible with Android API 23 and higher. Now

How can i solve it? Do i checked the android Version and write another code?

AsyncHttpClient client = new AsyncHttpClient();

        client.get(Constant_Api.home, null, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                Log.d("Response", new String(responseBody));
                String res = new String(responseBody);

                try {
                    JSONObject jsonObject = new JSONObject(res);

                    JSONObject jsonObjectBook = jsonObject.getJSONObject("EBOOK_APP");

                    JSONArray jsonArray = jsonObjectBook.getJSONArray("featured_books");

                    Log.e("TAG", "onSuccess: "+jsonArray.length() );

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

                        JSONObject object = jsonArray.getJSONObject(i);
                        String id = object.getString("id");
                        String cat_id = object.getString("cat_id");
                        String book_title = object.getString("book_title");

                        sliderList.add(new SubCategoryList(id, cat_id, book_title));

                    }

                    JSONArray jsonArrayLatestCat = jsonObjectBook.getJSONArray("category_list");
                    for (int i = 0; i < jsonArrayLatestCat.length(); i++) {

                        JSONObject object = jsonArrayLatestCat.getJSONObject(i);
                        String cid = object.getString("cid");
                        String category_name = object.getString("category_name");
                        String total_books = object.getString("total_books");
                        String cat_image=object.getString("category_image");

                        categoryLists.add(new CategoryList(cid, category_name, total_books,cat_image));

                    }

                    JSONArray jsonArrayLatest = jsonObjectBook.getJSONArray("latest_books");
                    for (int i = 0; i < jsonArrayLatest.length(); i++) {

                        JSONObject object = jsonArrayLatest.getJSONObject(i);
                        String id = object.getString("id");
                        String cat_id = object.getString("cat_id");
                        String book_title = object.getString("book_title");

                        latestList.add(new SubCategoryList(id, cat_id, book_title));

                    }

                    JSONArray jsonArrayPopular = jsonObjectBook.getJSONArray("popular_books");
                    for (int i = 0; i < jsonArrayPopular.length(); i++) {

                        JSONObject object = jsonArrayPopular.getJSONObject(i);
                        String id = object.getString("id");
                        String cat_id = object.getString("cat_id");
                        String book_title = object.getString("book_title");

                        mostPopularList.add(new SubCategoryList(id, cat_id, book_title));

                    }

                    latestAdapterGVLatest = new LatestHomeAdapterGV(getActivity(), latestList, "home_latest");
                    recyclerViewLatest.setAdapter(latestAdapterGVLatest);

                    latestAdapterGVPopular = new LatestHomeAdapterGV(getActivity(), mostPopularList, "home_most");
                    recyclerViewPopular.setAdapter(latestAdapterGVPopular);

                    categoryAdapter = new CategoryHomeAdapter(getActivity(), categoryLists);
                    recyclerViewCategory.setAdapter(categoryAdapter);

                    mAdapter = new CustomViewPagerAdapter();
                    mViewPager.setAdapter(mAdapter);
                    autoPlay(mViewPager);
                    progressBar.setVisibility(View.GONE);

                } catch (JSONException e) {
                    Log.e("TAG", "onSuccess: "+e);
                    Toast.makeText(getActivity(), ""+e, Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                Toast.makeText(getActivity(), "Failed Json", Toast.LENGTH_SHORT).show();
            }
        });
Imran Sk
  • 303
  • 5
  • 17
  • 1
    You have three choices... 1) Check API version and use different code on lower API versions 2) Change your `minSdkVersion` so that your app doesn't support lower API versions, or 3) Re-write the functionality in a way that supports all API versions you want to support. I would recommend using Kotlin instead of Java, and using Retrofit with GSON or Moshi, that way you can automatically get the response objects into Kotlin data classes. – Daniel Nugent Oct 08 '19 at 19:28
  • 1
    You could use Volley. It's easy and compatible with most API levels. – Squti Oct 08 '19 at 19:31
  • @DanielNugent Thank you for your valuable Comment. – Imran Sk Oct 08 '19 at 20:01

0 Answers0