Issue
I'm trying to display a list of names using an AsyncTask. doInBackground()
stores all the names found on a database in a String array.
public class GetAll extends AsyncTask<String, Void, String[]> {
public String convertStreamToString(InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
@Override
protected String[] doInBackground(String... apikey) {
String[] Students;
//Making a http call
HttpURLConnection urlConnection;
InputStream in = null;
try {
// the url we wish to connect to
URL url = new URL("http://radikaldesign.co.uk/sandbox/studentapi/getallstudents.php?apikey="+apikey);
// open the connection to the specified URL
urlConnection = (HttpURLConnection) url.openConnection();
// get the response from the server in an input stream
in = new BufferedInputStream(urlConnection.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
// convert the input stream to a string
String response = convertStreamToString(in);
// print the response to android monitor/log cat
System.out.println("Server response = " + response);
final ArrayList<Student> allStudents= new ArrayList<>();
try {
// declare a new json array and pass it the string response from the server
// this will convert the string into a JSON array which we can the iterate
// over using a loop
JSONArray jsonArray = new JSONArray(response);
// instantiate the cheeseNames array and set the size
// to the amount of cheese object returned by the server
Students = new String[jsonArray.length()];
// use a for loop to iterate over the JSON array
for (int i=0; i < jsonArray.length(); i++)
{
// the following line of code will get the name of the cheese from the
// current JSON object and store it in a string variable called name
String name = jsonArray.getJSONObject(i).get("name").toString();
String gender= jsonArray.getJSONObject(i).get("gender").toString();
String dob= jsonArray.getJSONObject(i).get("dob").toString();
String address= jsonArray.getJSONObject(i).get("address").toString();
String postcode= jsonArray.getJSONObject(i).get("postcode").toString();
String studentNumber= jsonArray.getJSONObject(i).get("studentNumber").toString();
String courseTitle= jsonArray.getJSONObject(i).get("courseTitle").toString();
String startDate= jsonArray.getJSONObject(i).get("startDate").toString();
String bursary= jsonArray.getJSONObject(i).get("bursary").toString();
String email= jsonArray.getJSONObject(i).get("email").toString();
Student s= new Student(name, gender, dob, address, postcode, studentNumber, courseTitle, startDate, bursary, email);
allStudents.add(s);
Students[i]= name;
return Students;
}
} catch (JSONException e) {
e.printStackTrace();
}
return new String[0];
}
Once the array is filled I want to display the results in a ListView on the MainActivity.
I've tried to store the results like using
String[] Students= new GetAll.execute(apikey);
and then use an ArrayAdapter to fill the listview. That did not work so I'm here for help and suggestions. Thanks
Solution
Adding to ADM's answer.. Create a inner class in the activity which extends Async Task which makes populating your original list after thread call simpler.
Call it as: new GetAll().execute("string");
Answered By - Shubham Agarwal Bhewanewala
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.