I'm doing an app for Android that makes a login to a server in MySQL , it must first go through a php file where it connects to the server and picks up the user and password of who is going to login. I made the php file that connects to the server (I'm going through steps that I'm new to this) so far, the browser returns that the server is connected.
<?php
$conn = new mysqli("127.0.0.1:3306", "root", "abc123.", "clients");
if($conn->connect_errno>0){
die('Imposible conectar['.$conn->connect_errno.']');
}else{
echo 'Conectado';
}
?>
In the Android app I added the php file of connected or not connected, but when I start the app on the physical mobile I jump to the catch of the class httpHandler, I guess it is not able to read the php. MainActivity :
public class WindowPrincipalLogin extends AppCompatActivity {
private TextView status;
private String txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.windowgraphic_pl);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
httpHandler han = new httpHandler();
txt = han.post("http://localhost/prueba/conn.php");
status.setText(txt);
Class that I call from the Main that executes the thread:
public class httpHandler {
public String post(String posturl){
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(posturl);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity ent = resp.getEntity();
String text = EntityUtils.toString(ent);
return text;
} catch (Exception e) {
return "Fallo en la conexión al servidor"+e.getMessage();
}
}