I am developing an application in Android Studio, where I will use the registered data of a web system based on PHP and Mysql. I was able to connect with the code below, I also managed to register directly through the application, but how would I do to bring the data in Android using PHP and Mysql?
Connection code
package br.com.perttutigestao.acessosistema;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Conexao {
public static String postDados(String urlUsuario, String parametrosUsuarios){
URL url;
HttpURLConnection connection = null;
try{
url = new URL(urlUsuario);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Lenght","" + Integer.toString(parametrosUsuarios.getBytes().length));
connection.setRequestProperty("Content-Language","pt-BR");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
outputStreamWriter.write(parametrosUsuarios);
outputStreamWriter.flush();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String linha;
StringBuffer resposta = new StringBuffer();
while ((linha = bufferedReader.readLine()) != null){
resposta.append(linha);
resposta.append('\r');
}
bufferedReader.close();
return resposta.toString();
}catch (Exception erro){
return null;
}finally {
if(connection != null){
connection.disconnect();
}
}
}
}
PHP
<?php
$conexao = mysqli_connect('localhost','root','success','projeto');
$sql = mysqli_query($conexao,"SELECT * FROM pe_mobile");
while($jm = mysqli_fetch_array($sql)){
$mostrar[] = $jm["Email"];
$mostrar[] = $jm["Senha"];
}
echo json_encode($mostrar);
I'm starting to develop in Android and I do not have much experience with Java.