So I've been making an android app, and at this point I am trying to send a GET request to retrieve a list of Poll data
{"authorID":"1","pollID":"1","status":"3","createdDate":"2019-11-10 13:06:32","patientID":"1","urgency":"3"}
After I retrieve the data I want to store it in an ArrayList of polls inside my pollsList.java class where I will display a listView of these polls.
While trying to do this I have found trouble setting the ArrayList and appending the retrieved poll values to it. I have come to understand that all volley requests are asynchronous and therefore I am not able to explicitly set them from inside the request code. With this finding I have now looked to create an AsyncTask implementation but I am still struggling to understand how data is passed in AsyncTest implementations.
Here is the code for the pollsList.java Activity
public class PollsList extends AppCompatActivity {
public PollsListAdapter adapter;
ArrayList<Poll> polls = new ArrayList<>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_polls);
ListView listView = (ListView)findViewById(R.id.list);
polls = new ArrayList<Poll>();
MyAsyncTask task = new MyAsyncTask(PollsList.this);
task.execute();
}
}
Below is the code for MyAsyncTask.java
public class MyAsyncTask extends AsyncTask<Void, Integer, Poll> {
static Context context;
public AsyncResponse delegate = null;
static ArrayList<Poll> polls = new ArrayList<>();
public MyAsyncTask(Context context){
this.context = context;
}
public interface AsyncResponse {
void processFinish(String output);
}
public MyAsyncTask(AsyncResponse delegate){
this.delegate = delegate;
}
@Override
protected Poll doInBackground(Void... voids) {
String url = "https://votingappapi.000webhostapp.com/VotingAppCrudAPI/Polls.php";
// Poll poll; if I initialize this poll object it must be final meaning it can't be modified
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Process the JSON
if (response.length() == 0) {
Toast.makeText(MyAsyncTask.context, "No Polls, Sorry", Toast.LENGTH_SHORT).show();
} else {
try {
JSONObject id = (JSONObject) response.get(1);
ArrayList<Poll> polls = new ArrayList();
// Poll poll;
for (int i = 0; i < response.length(); i++) {
JSONObject row = (JSONObject) response.get(i);
Poll poll = new Poll();
poll.authorID = row.getInt("authorID");
poll.urgency = row.getInt("urgency");
poll.patientID = row.getInt("patientID");
poll.status = row.getInt("status");
poll.pollID = row.getInt("pollID");
poll.createdDate = row.getString("createdDate");
polls.add(poll);
// cant return here
}
// can't return inside inner void class
// return poll;
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "JsonException: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Do something when error occurred
Log.d("TAG", "erorororor: " + error.getMessage());
}
}
);
}
@Override
protected void onPostExecute(Poll poll) {
Log.d("TAG", "New Poll " + poll + " created: " + poll.createdDate);
}
protected void onProgressUpdate(){
}
}
In the doInBackground method I am not sure how to return a poll. I would also like to ask which function handles the returned data from doInBackground once it is returned?
When I send the request for the list of polls should I return the poll of each iteration to pass on? Or should I wait for all Polls to be added to an arrayList before returning an arrayList instead?
Any help or guidance would be greatly appreciated!