JSON file parsing ran into errors in ANDROID

77 views Asked by At

I am trying to parse data from json using an API, but I don't know for what reasons when I am calculating the size of m_name3 it is showing 0 but the data is being parsed through the file. When putting toast at String name = o.getString("name"); it is showing the content of name string, so this means it is parsing the data but not storing in arraylist. I don't know why. Please help!

package com.example.amitc.pvrathome;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import okhttp3.OkHttpClient;

public class msms extends AppCompatActivity {
    String img_src;
    private ArrayList<String> m_name3 = new ArrayList<>();
    private ArrayList<String> m_release_year3 = new ArrayList<>();
    private ArrayList<String> m_genre3 = new ArrayList<>();
    private ArrayList<String> m_director3 = new ArrayList<>();
    private ArrayList<String> m_rating3 = new ArrayList<>();
    private ArrayList<String> m_actor3 = new ArrayList<>();
    private ArrayList<String> m_desc3 = new ArrayList<>();
    private ArrayList<String> m_imgsrc3 = new ArrayList<>();
    StringRequest stringRequest;
    Context mContext;
    String url_all = "https://www.dropbox.com/s/ofzv2j16vq9ofmq/100MovieList.json?dl=1";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.msms);

        new getIncomingIntent().execute();

        ImageView imageView = findViewById(R.id.msms_image);

        mContext = this;

        Glide.with(this)
                .load(img_src)
                .apply(new RequestOptions()
                        .placeholder(R.drawable.rec)
                        .dontAnimate()
                        .error(R.drawable.movies))
                .into(imageView);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //txt.setText("Searching by: "+ query);

        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {

            String uri = intent.getDataString();
            final int index = Integer.parseInt(uri.replaceAll("[^0-9]", ""));
            //


            stringRequest = new StringRequest(Request.Method.GET,
                    url_all,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                JSONArray array = jsonObject.getJSONArray("movies");

                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject o = array.getJSONObject(i);
                                    String name = o.getString("name");
                                    String year = o.getString("year");
                                    String genre = o.getString("genre");
                                    String rating = o.getString("rating");
                                    String director = o.getString("directors_name");
                                    String actor = o.getString("cast");
                                    String img_src = o.getString("poster_src");
                                    String desc = o.getString("description");

                                    m_name3.add(name);
                                    m_release_year3.add(year);
                                    m_genre3.add(genre);
                                    m_rating3.add(rating);
                                    m_director3.add(director);
                                    m_actor3.add(actor);
                                    m_imgsrc3.add(img_src);
                                    m_desc3.add(desc);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), "Some error Occured", Toast.LENGTH_SHORT).show();
                        }
                    });

            RequestQueue requestQueue = Volley.newRequestQueue(msms.this);
            requestQueue.add(stringRequest);

            Toast.makeText(this, "Index: "+ m_name3.size(), Toast.LENGTH_SHORT).show();

//            TextView m_name = findViewById(R.id.msms_name);
//            m_name.setText(m_name3.get(index));
//
//            TextView m_name1 = findViewById(R.id.msms_name1);
//            m_name1.setText(m_name3.get(index));
//            //        TextView m_year = (TextView) findViewById(R.id.msms_year);
//            //        m_year.setText(year);
//
//            TextView m_genre = findViewById(R.id.msms_genre);
//            m_genre.setText(m_genre3.get(index));
//
//            TextView m_rating = findViewById(R.id.msms_rating);
//            m_rating.setText(m_rating3.get(index));
//
//            TextView m_desc = findViewById(R.id.msms_desc);
//            m_desc.setText(m_desc3.get(index));
//
//            TextView m_direc = findViewById(R.id.msms_direc);
//            m_direc.setText(m_director3.get(index));
//
//            TextView m_actor = findViewById(R.id.msms_actor);
//            m_actor.setText(m_actor3.get(index));

//            Glide.with(this)
//                    .load(img_src)
//                    .apply(new RequestOptions()
//                            .placeholder(R.drawable.rec)
//                            .dontAnimate()
//                            .error(R.drawable.movies))
//                    .into(imageView);
        }

    }

    public class getIncomingIntent extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            String name = getIntent().getStringExtra("names");
//            String year = getIntent().getStringExtra("years");
            String genre = getIntent().getStringExtra("genres");
            String rating = getIntent().getStringExtra("ratings");
            String desc = getIntent().getStringExtra("decss");
            String director = getIntent().getStringExtra("direcs");
            String actor = getIntent().getStringExtra("stars");
            img_src = getIntent().getStringExtra("img1");


            TextView m_name = (TextView) findViewById(R.id.msms_name);
            m_name.setText(name);

            TextView m_name1 = (TextView) findViewById(R.id.msms_name1);
            m_name1.setText(name);
            //        TextView m_year = (TextView) findViewById(R.id.msms_year);
            //        m_year.setText(year);

            TextView m_genre = (TextView) findViewById(R.id.msms_genre);
            m_genre.setText(genre);

            TextView m_rating = (TextView) findViewById(R.id.msms_rating);
            m_rating.setText(rating);

            TextView m_desc = (TextView) findViewById(R.id.msms_desc);
            m_desc.setText(desc);

            TextView m_direc = (TextView) findViewById(R.id.msms_direc);
            m_direc.setText(director);

            TextView m_actor = (TextView) findViewById(R.id.msms_actor);
            m_actor.setText(actor);

            return null;
        }
    }
}

EDITED:

package com.example.amitc.pvrathome;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import okhttp3.OkHttpClient;

public class msms extends AppCompatActivity {
    String img_src;
    private ArrayList<String> m_name3 = new ArrayList<>();
    private ArrayList<String> m_release_year3 = new ArrayList<>();
    private ArrayList<String> m_genre3 = new ArrayList<>();
    private ArrayList<String> m_director3 = new ArrayList<>();
    private ArrayList<String> m_rating3 = new ArrayList<>();
    private ArrayList<String> m_actor3 = new ArrayList<>();
    private ArrayList<String> m_desc3 = new ArrayList<>();
    private ArrayList<String> m_imgsrc3 = new ArrayList<>();
    StringRequest stringRequest;
    Context mContext;
    String url_all = "https://www.dropbox.com/s/ofzv2j16vq9ofmq/100MovieList.json?dl=1";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.msms);

        new getIncomingIntent().execute();

        ImageView imageView = findViewById(R.id.msms_image);

        mContext = this;

        Glide.with(this)
                .load(img_src)
                .apply(new RequestOptions()
                        .placeholder(R.drawable.rec)
                        .dontAnimate()
                        .error(R.drawable.movies))
                .into(imageView);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //txt.setText("Searching by: "+ query);

        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {

            String uri = intent.getDataString();
            final int index = Integer.parseInt(uri.replaceAll("[^0-9]", ""));
            //


            stringRequest = new StringRequest(Request.Method.GET,
                    url_all,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String s) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                JSONArray array = jsonObject.getJSONArray("movies");

                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject o = array.getJSONObject(i);
                                    String name = o.getString("name");
                                    String year = o.getString("year");
                                    String genre = o.getString("genre");
                                    String rating = o.getString("rating");
                                    String director = o.getString("directors_name");
                                    String actor = o.getString("cast");
                                    String img_src = o.getString("poster_src");
                                    String desc = o.getString("description");

                                    m_name3.add(name);
                                    m_release_year3.add(year);
                                    m_genre3.add(genre);
                                    m_rating3.add(rating);
                                    m_director3.add(director);
                                    m_actor3.add(actor);
                                    m_imgsrc3.add(img_src);
                                    m_desc3.add(desc);
                                }

                                ProgressDialog progress;
                                progress= new ProgressDialog(mContext);
                                progress.setMessage("Loading...");
                                progress.show();

                                TextView m_name = findViewById(R.id.msms_name);
                                m_name.setText(m_name3.get(index));

                                TextView m_name1 = findViewById(R.id.msms_name1);
                                m_name1.setText(m_name3.get(index));
                                //        TextView m_year = (TextView) findViewById(R.id.msms_year);
                                //        m_year.setText(year);

                                TextView m_genre = findViewById(R.id.msms_genre);
                                m_genre.setText(m_genre3.get(index));

                                TextView m_rating = findViewById(R.id.msms_rating);
                                m_rating.setText(m_rating3.get(index));

                                TextView m_desc = findViewById(R.id.msms_desc);
                                m_desc.setText(m_desc3.get(index));

                                TextView m_direc = findViewById(R.id.msms_direc);
                                m_direc.setText(m_director3.get(index));

                                TextView m_actor = findViewById(R.id.msms_actor);
                                m_actor.setText(m_actor3.get(index));

                                ImageView imageView = findViewById(R.id.msms_image);

                                Glide.with(getApplicationContext())
                                            .load(img_src)
                                            .apply(new RequestOptions()
                                                    .placeholder(R.drawable.rec)
                                                    .dontAnimate()
                                                    .error(R.drawable.movies))
                                            .into(imageView);

                                progress.dismiss();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), "Some error Occured", Toast.LENGTH_SHORT).show();
                        }
                    });

            RequestQueue requestQueue = Volley.newRequestQueue(msms.this);
            requestQueue.add(stringRequest);
        }

    }

    public class getIncomingIntent extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            String name = getIntent().getStringExtra("names");
//            String year = getIntent().getStringExtra("years");
            String genre = getIntent().getStringExtra("genres");
            String rating = getIntent().getStringExtra("ratings");
            String desc = getIntent().getStringExtra("decss");
            String director = getIntent().getStringExtra("direcs");
            String actor = getIntent().getStringExtra("stars");
            img_src = getIntent().getStringExtra("img1");


            TextView m_name = (TextView) findViewById(R.id.msms_name);
            m_name.setText(name);

            TextView m_name1 = (TextView) findViewById(R.id.msms_name1);
            m_name1.setText(name);
            //        TextView m_year = (TextView) findViewById(R.id.msms_year);
            //        m_year.setText(year);

            TextView m_genre = (TextView) findViewById(R.id.msms_genre);
            m_genre.setText(genre);

            TextView m_rating = (TextView) findViewById(R.id.msms_rating);
            m_rating.setText(rating);

            TextView m_desc = (TextView) findViewById(R.id.msms_desc);
            m_desc.setText(desc);

            TextView m_direc = (TextView) findViewById(R.id.msms_direc);
            m_direc.setText(director);

            TextView m_actor = (TextView) findViewById(R.id.msms_actor);
            m_actor.setText(actor);

            return null;
        }
    }
}
1

There are 1 answers

5
Onur D. On BEST ANSWER

Since your stringRequest runs on a different thread, you can't be sure if you already have the response by the time you are showing the toast, so you should have a look at the size only after you have the response.

In short, move the toast inside of onResponse:

stringRequest = new StringRequest(Request.Method.GET,
                url_all,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray("movies");

                            for (int i = 0; i < array.length(); i++) {
                                JSONObject o = array.getJSONObject(i);
                                String name = o.getString("name");
                                String year = o.getString("year");
                                String genre = o.getString("genre");
                                String rating = o.getString("rating");
                                String director = o.getString("directors_name");
                                String actor = o.getString("cast");
                                String img_src = o.getString("poster_src");
                                String desc = o.getString("description");

                                m_name3.add(name);
                                m_release_year3.add(year);
                                m_genre3.add(genre);
                                m_rating3.add(rating);
                                m_director3.add(director);
                                m_actor3.add(actor);
                                m_imgsrc3.add(img_src);
                                m_desc3.add(desc);
                            }

                            Toast.makeText(msms.this, "Index: "+ m_name3.size(), Toast.LENGTH_SHORT).show();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), "Some error Occured", Toast.LENGTH_SHORT).show();
                    }
                });