1

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.

Banana
  • 2,435
  • 7
  • 34
  • 60
  • 2
    If you are just starting with Android and Java I can suggest this site https://www.androidhive.info. It has very simple basic examples to get you familiarized with Android. Good luck with Android :) – Banana Jan 26 '18 at 14:26
  • 1
    https://www.tutorialspoint.com/android/android_php_mysql.htm – Aswin P Ashok Jan 26 '18 at 15:54

1 Answers1

0

You can try to use Asynctask for getting data from web

  • 1
    Thanks for correcting Kemo. I'm from Brazil and I'm using an online translator for the text –  Jan 26 '18 at 14:22
  • Hello, Samuel. I do not have much experience in mobile development. Could you show me an example? –  Jan 26 '18 at 14:23
  • 1
    Hi, of course. You can get inspiration here: https://stackoverflow.com/a/18827536/6055278 – Samuel Senigl Jan 26 '18 at 14:25