0

Please Help Me where I'm wrong. I'm new and tired with this problem. I have a class ForcastTask extends AsyncTask, another class ForcastFragment extends Fragment implements AsyncResponse and Main Activity class.

I want to get onPostExcute(List data) from AsyncTask class and send to ForcastFragment class by using AsyncResponse interface.

But it only show null, I checked my query parameter in json viewer online it works and when I try it in single class then it work properly.

public class ForecastTask extends AsyncTask<String, String, List<MovieModel>> {
    private final String LOG_TAG = ForecastTask.class.getSimpleName();    
    public AsyncResponse delegate=null;
    public List<MovieModel> movieModelList=null;
    @Override
    protected List<MovieModel> doInBackground(String... params) {
        if (params.length == 0) {
            return null;
        }

        String Popularity = "http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=XXXXXxxxxxxxxxxxxx";

        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(Popularity);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            InputStream inputStream = connection.getInputStream();
            if (inputStream == null) {
                return null;
            }

            reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            JSONObject jsonObject = new JSONObject(buffer.toString());
            JSONArray jsonArray = jsonObject.getJSONArray("results");

            //adding JSON Array data into MovieModel Class
            movieModelList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject finalObject = jsonArray.getJSONObject(i);
                MovieModel movieModel = new MovieModel();
                    movieModel.setId(finalObject.getInt("id"));
                    movieModel.setTitle(finalObject.getString("title"));
                    movieModel.setPoster_path(finalObject.getString("poster_path"));
                    movieModel.setRelease_date(finalObject.getString("release_date"));
                    movieModel.setVote_average((float) finalObject.getDouble("vote_average"));
                    movieModel.setOverview(finalObject.getString("overview"));
                movieModelList.add(movieModel);
            }

            return movieModelList;
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
        return null;
    }

    @Override
    protected void onPostExecute(List<MovieModel> movieModels) {
        delegate.processFinish(movieModels);
    }
}

2- ForecastFragment class

public class ForecastFragment extends Fragment implements AsyncResponse{
    private List<MovieModel> movieModelList=null;
    private static final String STATE_MOVIES ="state_movies";
    private CustomAdapter customAdapter =null;
    private GridView gridView=null;
    private View rootView=null;

    ForecastTask forecastTask=new ForecastTask();

    //Constructor
    public ForecastFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        forecastTask.delegate=this;
        forecastTask.execute("Popular");
    }
    @Override
    public void processFinish(List<MovieModel> movieModels) {
        movieModelList=movieModels;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(STATE_MOVIES, (ArrayList<? extends Parcelable>) movieModelList);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.most_Popular:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    new ForecastTask().execute("Popular");
                    return true;
                }
            case R.id.High_rated:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    new ForecastTask().execute("TOP_RATED");
                    return true;
                }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_main, container, false);

        customAdapter= new CustomAdapter(
                getActivity(),
                R.layout.list_image_forecast,
                movieModelList);
        gridView=(GridView)rootView.findViewById(R.id.gridView);

        if (movieModelList==null){
            Toast.makeText(getActivity(),"Null",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getActivity(), "Data", Toast.LENGTH_SHORT).show();

            gridView.setAdapter(customAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String movie_name = movieModelList.get(position).getTitle();
                    String poster_path = movieModelList.get(position).getPoster_path();
                    String release_date = movieModelList.get(position).getRelease_date();
                    Float users_rating = movieModelList.get(position).getVote_average();
                    String overview = movieModelList.get(position).getOverview();

                    Intent intent = new Intent(getActivity(), DetailActivity.class)
                            .putExtra("movie_Name", movie_name)
                            .putExtra("poster_Path", poster_path)
                            .putExtra("release_Date", release_date)
                            .putExtra("users_Rating", users_rating)
                            .putExtra("overview", overview);
                    startActivity(intent);
                }
            });
        }
        return rootView;
    }
}

3- Async interface

public interface AsyncResponse {
    void processFinish(List<MovieModel> movieModels);
}

4-Movie DB

public class MovieModel implements Parcelable {
    private int id;
    private String title;
    private String poster_path;
    private String release_date;
    private float vote_average;
    private String overview;

    private List<MovieModel> models;
    public MovieModel(){

    }

    public List<MovieModel> getModels() {
        return models;
    }

    public void setModels(List<MovieModel> models) {
        this.models = models;
    }

    public MovieModel(int id, String title, String poster_path, String release_date, float vote_average, String overview) {
        this.id = id;
        this.title = title;
        this.poster_path = poster_path;
        this.release_date = release_date;
        this.vote_average = vote_average;
        this.overview = overview;
    }

    protected MovieModel(Parcel in) {
        id = in.readInt();
        title = in.readString();
        poster_path = in.readString();
        release_date = in.readString();
        vote_average = in.readFloat();
        overview = in.readString();
    }

    public static final Creator<MovieModel> CREATOR = new Creator<MovieModel>() {
        @Override
        public MovieModel createFromParcel(Parcel in) {
            return new MovieModel(in);
        }

        @Override
        public MovieModel[] newArray(int size) {
            return new MovieModel[size];
        }
    };

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getPoster_path() {
        return poster_path;
    }
    public void setPoster_path(String poster_path) {
        this.poster_path = poster_path;
    }

    public String getRelease_date() {
        return release_date;
    }
    public void setRelease_date(String release_date) {
        this.release_date = release_date;
    }

    public float getVote_average() {
        return vote_average;
    }
    public void setVote_average(float vote_average) {
        this.vote_average = vote_average;
    }

    public String getOverview() {
        return overview;
    }
    public void setOverview(String overview) {
        this.overview = overview;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeString(title);
        out.writeString(poster_path);
        out.writeString(release_date);
        out.writeFloat(vote_average);
        out.writeString(overview);
    }

}

5- Main Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Call the ForecastFragment Class by getFragmentManager
        getFragmentManager()
                .beginTransaction()
                .add(R.id.Fragment_Container, new ForecastFragment(),
                        ForecastTask.class.getSimpleName())
                .commit();
    }
Santosh
  • 227
  • 1
  • 4
  • 18

2 Answers2

0

You're only assigning delegate once in the forecastTask defined as a member of the fragment. But you create multiple tasks in other places without assigning their delegate. You probably need to assign delegate for each one of the tasks separately for each task to make changes in onPostExecute, or it will crash with a NullPointerException.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Could you please give me one example. I try it, or please take a look my github link : https://github.com/superssingh/MyMovieApp_Testing.git – Santosh Mar 16 '16 at 20:41
0

I would recommend you to pass an object of your AsyncResponse to the ForecastTask constructor.

public ForecastTask(AsyncResponse listener){
    delegate = listener;
}

and in your fragment you can do this.

ForecastTask forecastTask = new ForecastTask(this);
forecastTask.execute("Popular");
Rohan Arora
  • 661
  • 5
  • 15
  • I try but it doesn't works. please take a look my github account where I am wrong link : github.com/superssingh/MyMovieApp_Testing.git – Santosh Mar 16 '16 at 20:44