I have a php file which fetches all rows from a specific column. The result is then put into an array and then json_encoded. When I Toast the output from the php file it shows a JSON string with the correct values. I need to convert this to a Java String array so I can iterate through it one by one on a button click.
I've tried multiple solutions on here but to no avail so any help will be greatly appreciated!
getImage.php
$sql = "SELECT advert_File_Path FROM Ads";
$result = $mysql_con->query($sql) or die('Query problem: '.mysqli_error($mysql_con));
//create array
$resultarray = array();
while($row = mysqli_fetch_assoc($result)){
$resultarray[] = $row;
}
echo json_encode($resultarray);
MainActivity.java (only the code that is relevant)
private void loadNextAdvert()
{
Toast.makeText(this, "Please wait, image may take a few seconds to load...", Toast.LENGTH_LONG).show();
dbhelper = (DbHelper) new DbHelper(new DbHelper.AsyncResponse() {
@Override
public void processFinish(String output) {
if(output.equalsIgnoreCase("exception") || output.equalsIgnoreCase("unsuccessful")){
Toast.makeText(MainActivity.this, "Connection error", Toast.LENGTH_SHORT).show();
}else{
//initializes ArrayList
stringList = new ArrayList<>();
try {
//initializes JSONArray with output from php getImage file
jsonArray = new JSONArray(output);
if(jsonArray != null){
int len = jsonArray.length();
for(int i=0; i<len; i++){
stringList.add(jsonArray.get(i).toString());
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, String.valueOf(stringList), Toast.LENGTH_LONG).show();
}
}
}).execute(loadPic);
}
The Toast which should display the contents of the ArrayList just appears as '[]'.