First of all, you will need a connection class, I use this in my projects with help with php:
public class Conexao {
public static String postDados(String urlUsuario, String parametrosUsuario) {
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;charset=utf-8");
connection.setRequestProperty("Content-Lenght", "" + Integer.toString(parametrosUsuario.getBytes().length));
connection.setRequestProperty("Content-Language", "pt-BR");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStreamWriter outPutStream = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
outPutStream.write(parametrosUsuario);
outPutStream.flush();
outPutStream.close();
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();
}
}
}
}
Ready, now to use it you do this:
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String texto = "123";
url = "url_do_seu_arquivo_php"; //Caso queira um get, só colocar na url
// ?variavel=valor
//Parâmentros por post
parametros = "texto=" + texto;
new SolicitaDados().execute(url);
}
private class SolicitaDados extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return Conexao.postDados(urls[0], parametros);
}
@Override
protected void onPostExecute(String resultado) {
//String resultado tem o retorno
}
}
}
So you send the posts and gets to your php file, for example, and there you send the data to mysql, if you want to receive something, in the url you put the path with php file and pass the necessary parameters for this print the data, the entire answer comes in the String "response" in the onPostExecute