Search code examples
androidandroid-asynctaskandroid-5.0-lollipopjson

JSON doesn't work on Android Lollipop


In my project I've created a listview with data shown from JSON (via BaseAdapter). On the initialize of the Adapter I initialize AsyncTask containing the call for JSON with Progress Dialog shown.

In all android version everything works fine, but in android lollipop the loading just doesn't stop.

private class NewsJsonAsync extends AsyncTask<Void, Void, Boolean> {

    private String mURLString = "http://json.com/json.json";
    private ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setMessage(mContext.getString(R.string.dialog_loading_title));
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();

    }

    @Override
    protected Boolean doInBackground(Void... params) {

        Log.d("DO IN BACKGROUND", "BEFORE TRY/CATCH");
        try {
            String stream = streamManager();
            jsonManager(stream);

        } catch (JSONException e) {
            e.printStackTrace();
            Log.d("JSON EXCEPTION HERE", e.getLocalizedMessage());
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("IO EXCEPTION HERE", e.getLocalizedMessage());
            return false;
        }
        Log.d("DO IN BACKGROUND", "AFTER TRY/CATCH");
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (result == true) {
            notifyDataSetChanged();
            mProgressDialog.dismiss();
        } else {
            mProgressDialog.dismiss();
        }
    }

    private String streamManager() throws IOException {
        URL url = new URL(mURLString);
        InputStream stream = url.openStream(); //open the data stream
        byte[] bytes = new byte[stream.available()];

        String getBytes = ""; //holds the data recived for the server in their raw form
        int countBytes; //indicator for the amount of bytes left

        while ((countBytes = stream.read(bytes)) > -1 ) { //while the stream size is more then -1 (while there is still data)
            getBytes += new String(bytes,0,countBytes); //add the data to the string

        }
        stream.close();
        return getBytes;
    }


    protected void jsonManager (String data) throws JSONException {
        JSONArray jsonArray = new JSONArray(data); //call the JSON GENERAL array

        String title = "";
        String subtitle = "";

        int size = jsonArray.length();

        for (int i = 0; i < size; i++) {

            JSONObject jsonArticle = jsonArray.getJSONObject(i);
            title = jsonArticle.getString(Enums.JSON_TITLE.getValue());
            subtitle = jsonArticle.getString(Enums.JSON_SUBTITLE.getValue());


            mArray.add(new NewsListWithDB(title, subtitle)));
        }

I stop getting any logs in "BEFORE TRY/CATCH". Following the Log-cat I think it might have something to do with one message I get repeatedly

12-10 09:38:38.991: I/art(23270): Background sticky concurrent mark sweep GC freed 263748(8MB) AllocSpace objects, 0(0B) LOS objects, 33% free, 10MB/16MB, paused 1.617ms total 107.675ms

Could it be that ART is the source of the problem? what should I do?


Solution

  • Okay, so after looking for an answer it seems the problem isn't with my code, the problem is with android 5.

    I tried many tutorials of JsonArray/JsonObject, downloaded their samples etc but nothing worked. Also, I've witnessed a growing number of developer who suffer from the same problem.

    Thanks to Lena's advice I tried GSON again, and this time it worked. The last time I tired using GSON the problem was my poor implementation.

    Thank you all.