Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. package com.ghoststeam.kbsurate;
  2.  
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import androidx.annotation.NonNull;
  10. import androidx.annotation.Nullable;
  11. import androidx.fragment.app.Fragment;
  12. import androidx.recyclerview.widget.LinearLayoutManager;
  13. import androidx.recyclerview.widget.RecyclerView;
  14.  
  15. import org.jsoup.Jsoup;
  16. import org.jsoup.nodes.Document;
  17. import org.jsoup.nodes.Element;
  18. import org.jsoup.select.Elements;
  19.  
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23.  
  24. public class pointsFragment extends Fragment {
  25. private RecyclerView cardList;
  26. private Runnable getInfoFrom;
  27. private Thread studInfo;
  28. private Handler setMyAdapter;
  29. private List<Bundle> bundles = new ArrayList<>();
  30. private int size;
  31. private String link;
  32. private static final int TIMEOUT = 200000;
  33. private static final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36";
  34. private boolean generated = false;
  35.  
  36. public pointsFragment (String link)
  37. {
  38. this.link = link;
  39. }
  40.  
  41. @Nullable
  42. @Override
  43. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  44. View pointsView = inflater.inflate(R.layout.points_fragment, container, false);
  45. if (!generated) {
  46. cardList = pointsView.findViewById(R.id.cardlist_view);
  47. cardList.setLayoutManager(new LinearLayoutManager(getActivity()));
  48. cardList.setHasFixedSize(true);
  49.  
  50. setMyAdapter = new Handler() {
  51. public void handleMessage(Message msg) {
  52. cardsAdapter card = new cardsAdapter(size);
  53. card.getBundlesFrom(bundles);
  54. cardList.setAdapter(card);
  55. }
  56. };
  57.  
  58.  
  59. getInfoFrom = new Runnable() {
  60. @Override
  61. public void run() {
  62. getInfoFromServer(link);
  63. }
  64. };
  65.  
  66. studInfo = new Thread(getInfoFrom);
  67. studInfo.start();
  68.  
  69. }
  70. return pointsView;
  71. }
  72.  
  73. private void getInfoFromServer(String link) {
  74. Bundle bundle = new Bundle();
  75. Document doc = null;
  76. try {
  77. doc = Jsoup.connect(link).timeout(TIMEOUT).userAgent(userAgent).get();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. if (doc == null) {
  82. return;
  83. }
  84. Elements mainTable = doc.select("table");
  85. Elements mainRows = mainTable.select("tr");
  86. Elements mainCols = mainRows.select("td");
  87. Element subTable = mainCols.select("table").first();
  88. Elements rows = subTable.select("tr");
  89. size = rows.size() - 2;
  90. for (int i = 2; i < rows.size(); i++) {
  91. ArrayList<String> firstPoint = new ArrayList<>();
  92. ArrayList<String> secondPoint = new ArrayList<>();
  93. ArrayList<String> thirdPoint = new ArrayList<>();
  94. Element row = rows.get(i);
  95. Elements cols = row.select("td");
  96. cols.get(1).select("b");
  97. String exam = cols.get(1).select("b").text();
  98. bundle.putString("Examination", exam);
  99. cols.get(1).select("b").remove();
  100. bundle.putString("Subject", cols.get(1).text());
  101. bundle.putString("Value", cols.get(21).text());
  102. for (int j = 3; j < 7; j++)
  103. firstPoint.add(cols.get(j).text());
  104. for (int k = 8; k < 12; k++)
  105. secondPoint.add(cols.get(k).text());
  106. for (int l = 13; l < 17; l++)
  107. thirdPoint.add(cols.get(l).text());
  108. bundle.putStringArrayList("First point", firstPoint);
  109. bundle.putStringArrayList("Second point", secondPoint);
  110. bundle.putStringArrayList("Third point", thirdPoint);
  111. bundles.add(new Bundle(bundle));
  112. }
  113. generated = !generated;
  114. setMyAdapter.sendEmptyMessage(1);
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement