0
I’m developing an app on Android where the data is in a web-based developed in PHP/Mysql. The code I’m 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 in the database have 04 records:
And PHP being that way:
<?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?
In the Arrayjson tab, returns the following:
This is Fox. So maybe you already have, but have tried debugging and inspecting Arrayjson JA?
– MarlonJhow
Hi Marlon. I haven’t tried that yet. For me to debug, would it be inside Android Studio itself? I ask, because I’m starting now to work with this IDE and I don’t have much experience in it.
– user24136
Yes, it’s only time to build you choose the bug icon button, then put a break point on the variable line and re-test the operation. When you go through the line the IDE will freeze the processing and you will be able to inspect what is inside the variable
– MarlonJhow
Right. I debugged and put the print in the post. It looks like it’s returning the values correctly.
– user24136