App only keeps a record on Android

0

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:

    
asked by anonymous 26.01.2018 / 23:32

1 answer

-1

I'm not sure and I can not confirm at the moment, but if I'm not mistaken you can use mysqli_fetch_all() , which will already return all the desired records in an array, so you can send directly as JSON to the response of the request.

The idea would be this:

<?php
$conexao = mysqli_connect('localhost','root','sucesso','projetos');
$sql = mysqli_query($conexao,"SELECT * FROM pe_mobile");
echo json_encode( mysqli_fetch_all( $sql, \MYSQLI_ASSOC ) );

It would be interesting if you set the response header to JSON too:

header( 'Content-Type: application/json' );//antes do echo

And for development it would be interesting to enable error messages:

error_reporting( E_ALL );//primeira linha de código após abertura do <?php

See these examples from the manual as well: link

    
27.01.2018 / 01:10