Skip to content

Instantly share code, notes, and snippets.

@hoc081098
Created March 30, 2024 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoc081098/4069c879b2747999be83aa83ef283ff7 to your computer and use it in GitHub Desktop.
Save hoc081098/4069c879b2747999be83aa83ef283ff7 to your computer and use it in GitHub Desktop.
private void API_Get_List() {
ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());
executor.execute(new Runnable() {
@Override
public void run() {
final StringRequest request = new StringRequest(Request.Method.POST,
"https://primex11.live/game/Api/contest_player_details", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
listModels.clear();
Log.d("TAG", response.toString());
try {
JSONObject obj = new JSONObject(response);
int success = obj.getInt("status");
long start = System.currentTimeMillis();
System.out.println(">>> DONE API start=" + start);
if (success != Appconfig.TAG_SUCCESS) {
// Handle unsuccessful response
} else {
JSONArray arr = obj.getJSONArray("data");
for (int i = 0; arr.length() > i; i++) {
JSONObject obj1 = arr.getJSONObject(i);
apilist_contestId = obj1.getString("contest_play_id");
apilist_teamName = obj1.getString("team_name");
apilist_playerId1 = obj1.getString("player1_id");
apilist_playerName1 = obj1.getString("player1");
apilist_playerPoints1 = "0";
apilist_playerId2 = obj1.getString("player2_id");
apilist_playerName2 = obj1.getString("player2");
apilist_playerPoints2 = "0";
apilist_playerId3 = obj1.getString("player3_id");
apilist_playerName3 = obj1.getString("player3");
apilist_playerPoints3 = "0";
apilist_playerId4 = obj1.getString("player4_id");
apilist_playerName4 = obj1.getString("player4");
apilist_playerPoints4 = "0";
apilist_playerId5 = obj1.getString("player5_id");
apilist_playerName5 = obj1.getString("player5");
apilist_playerPoints5 = "0";
apilist_playerId6 = obj1.getString("player6_id");
apilist_playerName6 = obj1.getString("player6");
apilist_playerPoints6 = "0";
apilist_playerId7 = obj1.getString("player7_id");
apilist_playerName7 = obj1.getString("player7");
apilist_playerPoints7 = "0";
apilist_playerId8 = obj1.getString("player8_id");
apilist_playerName8 = obj1.getString("player8");
apilist_playerPoints8 = "0";
apilist_playerId9 = obj1.getString("player9_id");
apilist_playerName9 = obj1.getString("player9");
apilist_playerPoints9 = "0";
apilist_playerId10 = obj1.getString("player10_id");
apilist_playerName10 = obj1.getString("player10");
apilist_playerPoints10 = "0";
apilist_playerId11 = obj1.getString("player11_id");
apilist_playerName11 = obj1.getString("player11");
apilist_playerPoints11 = "0";
apilist_pointsTotal = "0";
listModels.add(new ListModel(apilist_teamName, apilist_playerId1, apilist_playerName1, apilist_playerPoints1, apilist_playerId2, apilist_playerName2, apilist_playerPoints2, apilist_playerId3, apilist_playerName3, apilist_playerPoints3, apilist_playerId4, apilist_playerName4, apilist_playerPoints4, apilist_playerId5, apilist_playerName5, apilist_playerPoints5, apilist_playerId6, apilist_playerName6, apilist_playerPoints6, apilist_playerId7, apilist_playerName7, apilist_playerPoints7, apilist_playerId8, apilist_playerName8, apilist_playerPoints8, apilist_playerId9, apilist_playerName9, apilist_playerPoints9, apilist_playerId10, apilist_playerName10, apilist_playerPoints10, apilist_playerId11, apilist_playerName11, apilist_playerPoints11));
//DATABASE
dblistModels.add(new DbListModel(apilist_contestId, apilist_teamName, apilist_playerId1, apilist_playerName1, apilist_playerPoints1, apilist_playerId2, apilist_playerName2, apilist_playerPoints2, apilist_playerId3, apilist_playerName3, apilist_playerPoints3, apilist_playerId4, apilist_playerName4, apilist_playerPoints4, apilist_playerId5, apilist_playerName5, apilist_playerPoints5, apilist_playerId6, apilist_playerName6, apilist_playerPoints6, apilist_playerId7, apilist_playerName7, apilist_playerPoints7, apilist_playerId8, apilist_playerName8, apilist_playerPoints8, apilist_playerId9, apilist_playerName9, apilist_playerPoints9, apilist_playerId10, apilist_playerName10, apilist_playerPoints10, apilist_playerId11, apilist_playerName11, apilist_playerPoints11, apilist_pointsTotal));
// Do something with the parsed data
}
System.out.println(">>> done loop " + (System.currentTimeMillis() - start) + "ms");
dbViewModel.insertData(dblistModels);
handler.post(new Runnable() {
@Override
public void run() {
recyclerViewDb.setLayoutManager(new LinearLayoutManager(MainActivity.this));
dbViewModel.getAllLeaderBoardFromDB().observe(MainActivity.this, dbModels ->
{
long end = System.currentTimeMillis();
System.out.println(">>> end=" + end);
if (dbModels != null && !dbModels.isEmpty()) {
dbListAdapter = new DbListAdapter(MainActivity.this, (ArrayList<DbListModel>) dbModels);
recyclerViewDb.setAdapter(dbListAdapter);
int storeList = dbListAdapter.getItemCount();
txt_total.setText(String.valueOf(storeList));
int getcount = dbListAdapter.getItemCount();
if (getcount >= 0) {
recyclerView.setVisibility(View.GONE);
}
}
});
listAdapter = new ListAdapter(getApplicationContext(), listModels);
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error response
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("contest_id", "34");
return params;
}
};
// Adding request to request queue
request.setRetryPolicy(new DefaultRetryPolicy(
Appconfig.TAG_VOLLERY_TIMEOUT,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(request);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment