What is the best technology to establish a connection between an android application and a server (in my case PHP, but this is secondary), REST, Socket, WebSocket, SOAP, ...? (Just mention the most used or best, I do not need explanations).
I am currently starting to use HttpUrlConnection()
. I send from my Android application, using the POST method, a form to a PHP file on the server, to store certain data in a database.
I do not know how I can do to, in the android application, get the response from the web page, that is, if there has been any problem in the insertion of data (duplicate records, some wrong format, ...).
I attach the code (works) of the connection for the insertion of the data:
private void enviarDatos() throws IOException {
//Lo siguiente solo es temporal, para que me deje hacer pruebas en el hilo principal
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
URL url = new URL("http://"+miip+":8080/insert.php");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(15000);
urlConnection.setConnectTimeout(15000);
String data = "nombre="+nombre.getText().toString()+"&"+"apellidos="+apellidos.getText().toString()+"&"+"pass="+pass.getText().toString()+"&"+"email="+email.getText().toString();
Toast.makeText(getApplicationContext(),data,Toast.LENGTH_LONG).show();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setFixedLengthStreamingMode(data.getBytes().length);
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(data.getBytes());
out.flush();
out.close();
} else {
Toast.makeText(getApplicationContext(),"No existe conexión",Toast.LENGTH_SHORT).show();
//Mostrar errores
}
}
It is for a user registry. Is it possible to get a response to the submission of the form in this way?