I'm developing an app on Android where data is in a web-based developed in PHP / Mysql. The code I am using to get the data is:
public class ListarDados extends AppCompatActivity {
public static TextView data;
//String url = ""; //
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listar_dados);
data = (TextView) findViewById(R.id.fetchedata);
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
new SolicitaDados().execute();
}else {
Toast.makeText(getApplicationContext(), "Nenhuma conexão ativa", Toast.LENGTH_LONG).show();
}
}
private class SolicitaDados extends AsyncTask<Void, Void, Void> {
String data = "";
String dataParsed = "";
String singleParsed = "";
@Override
protected Void doInBackground(Void... voids){
try {
URL url = new URL("http://192.168.0.13/plataforma/android/listar.php");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String linhas = "";
while(linhas != null){
linhas = bufferedReader.readLine();
data = data + linhas;
}
JSONArray JA = new JSONArray(data);
for(int i = 0; i < JA.length(); i++){
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "Email:" + JO.get("Email") + "Senha:" + JO.get("Senha") + "\n";
dataParsed = dataParsed + singleParsed + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
ListarDados.data.setText(this.dataParsed);
}
}
@Override
protected void onPause() {
super.onPause();
Intent voltar = new Intent(ListarDados.this, ConteudoSistema.class);
startActivity(voltar);
}
}
Although the database has 04 records:
AndPHPlookslikethis:
<?php$conexao=mysqli_connect('localhost','root','sucesso','projetos');$sql=mysqli_query($conexao,"SELECT * FROM pe_mobile");
$mostrar = array();
//$mostrar["dados"] = array();
foreach($sql as $linha){
$listar["Email"] = $linha["Email"];
$listar["Senha"] = $linha["Senha"];
array_push($mostrar,$listar);
}
echo json_encode($mostrar);
I have as a result:
[{"Email":"[email protected]","Senha":"123"},{"Email":"[email protected]","Senha":"321"},{"Email":"[email protected]","Senha":"456"},{"Email":"[email protected]","Senha":"654"}]
But when I run the app, it only returns me the first record. How do I bring all the records?
At the root of the ArrayJSON, it returns the following: