'How to fetch Realtime data from website using jsoup in android

I fetch website data successfully but the problem is websites update they're per 10 seconds. but my app does not fetch updated data without restarting activity.

I try using Handler but updated the data set on the adapter before the old data.

  final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Content content = new Content();
            content.execute();

            handler.postDelayed(this,1000);
        }
    };
    handler.postDelayed(runnable,1000);

My Fragment

  progressBar = v.findViewById(R.id.progressBar1);
    recyclerView = v.findViewById(R.id.recyclerView1);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    adapter = new LiveAdapter(parseItems, getContext());
    recyclerView.setAdapter(adapter);

    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Content content = new Content();
            content.execute();

            handler.postDelayed(this,1000);
        }
    };
    handler.postDelayed(runnable,1000);



    Content content = new Content();
    content.execute();

    return v;
}
private class Content extends AsyncTask<Void, Void, Void> {

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

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressBar.setVisibility(View.GONE);
        progressBar.startAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out));
        adapter.notifyDataSetChanged();
    }

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            String url = "https://www.cricketworld.com/cricket/live";

            Document doc = Jsoup.connect(url).get();


            Elements div = doc.select("div.matches-table");  // select your div with yor class
            Elements el = div.select("div.widget-content > a.match-format-2");

            //- Elements ul = div.select("div.col-md-8");
            // Elements r = ul.select("div.col-12");

            int size = el.size();
            Log.d("doc", "doc: " + doc);
            Log.d("data", "data: " + div);
            Log.d("size", "" + size);
            for (int i = 0; i < size; i++) {



                String matchType = el.get(i).select("div.column-meta > div.date > span.subtitle").text();
                String firstImage = el.get(i).select("div.teama > div.teamLogo").select("img").attr("src");
                String secondImage = el.get(i).select("div.teamb > div.teamLogo").select("img").attr("src");

                String firstTeamName = el.get(i).select("div.teama > div.teamName").text();
                String secondTeamName = el.get(i).select("div.teamb > div.teamName").text();

                String teamAScore = el.get(i).select("div.teama > div.teamScore").text();
                String teamBScore = el.get(i).select("div.teamb > div.teamScore").text();

                String clickURL = el.get(i).attr("href");

                String matchDate = el.get(i).select("div.column-meta > div.date > span.day").text();
                String matchTime = el.get(i).select("div.status_note").text();

                parseItems.add(new LiveModels(firstImage, secondImage, matchType, matchDate, matchTime, firstTeamName, secondTeamName, teamAScore, teamBScore, clickURL));
                Log.d("items", "img: " + matchType + " . title: " + matchType);

            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }
}

Adapter

public class LiveAdapter extends RecyclerView.Adapter<LiveAdapter.ViewHolder> {

ArrayList<LiveModels> seriesItems;
Context context;

public LiveAdapter(ArrayList<LiveModels> seriesItems, Context context) {
    this.seriesItems = seriesItems;
    this.context = context;

}

public void updateData(ArrayList<LiveModels> seriesItems) {
    this.seriesItems.clear();
    if (seriesItems != null) {
        this.seriesItems.addAll(seriesItems);
    }
    this.notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_live, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    holder.matchTitle.setText(seriesItems.get(position).getMatchTitle());
    holder.matchDate.setText(seriesItems.get(position).getMatchDate());

    String ddd = seriesItems.get(position).getMatchTime().substring(21);

    if (!ddd.equals("")) {

        holder.matchTime.setText(ddd);
    } else {
        holder.matchTime.setText("Match ended");
    }

    // Score Setup
    String teamAScore = seriesItems.get(position).getTeamAScore();
    if (!teamAScore.equals("")) {
        holder.teamAScore.setText(teamAScore.replace("& ", "\n"));
    }
    String teamBScore = seriesItems.get(position).getTeamBScore();
    if (!teamBScore.equals("")) {
        holder.teamBScore.setText(teamBScore.replace("& ", "\n"));
    }


    // Imageview Setup
    if (!seriesItems.get(position).getFirstTeamImage().isEmpty()) {
        Picasso.get().load(seriesItems.get(position).getFirstTeamImage()).placeholder(R.drawable.progress).into(holder.firstTeamImage);
    } else {
        holder.firstTeamImage.setImageResource(R.drawable.ic_launcher_background);
    }
    if (!seriesItems.get(position).getSecondTeamName().isEmpty()) {
        Picasso.get().load(seriesItems.get(position).getSecondTeamImage()).placeholder(R.drawable.progress).into(holder.secondTeamImage);
    } else {
        holder.secondTeamImage.setImageResource(R.drawable.ic_launcher_background);
    }

    // Team name Setup
    if (!seriesItems.get(position).getFirstTeamName().isEmpty()) {

        String teamName = seriesItems.get(position).getFirstTeamName().replace("Women", "W");
        holder.firstTeamName.setText(teamName);
    }
    if (!seriesItems.get(position).getSecondTeamName().isEmpty()) {

        String teamName = seriesItems.get(position).getSecondTeamName().replace("Women", "W");
        holder.secondTeamName.setText(teamName);
    }


}

@Override
public int getItemCount() {
    return seriesItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    CircleImageView firstTeamImage, secondTeamImage;
    TextView matchTitle, matchDate, matchTime, firstTeamName, secondTeamName, teamAScore, teamBScore;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        firstTeamImage = itemView.findViewById(R.id.firstTeamImage);
        secondTeamImage = itemView.findViewById(R.id.secondTeamImage);

        matchTitle = itemView.findViewById(R.id.matchTitle);
        matchDate = itemView.findViewById(R.id.matchDate);
        matchTime = itemView.findViewById(R.id.matchTime);
        firstTeamName = itemView.findViewById(R.id.firstTeam);
        secondTeamName = itemView.findViewById(R.id.secondTeam);
        teamAScore = itemView.findViewById(R.id.teamAScore);
        teamBScore = itemView.findViewById(R.id.teamBScore);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(itemView.getContext(), DetailsLiveScore.class);
                intent.putExtra("url", seriesItems.get(getAdapterPosition()).getClickUrl());
                intent.putExtra("teamA", seriesItems.get(getAdapterPosition()).getFirstTeamName() + " vs " + seriesItems.get(getAdapterPosition()).getSecondTeamName());
                intent.putExtra("matchStatus", seriesItems.get(getAdapterPosition()).getMatchTitle());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                itemView.getContext().startActivity(intent);
            }
        });

    }
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source