Home > Mobile >  Unable to connect to the PHP android to verify the user name and password
Unable to connect to the PHP android to verify the user name and password

Time:10-04

Android use HttpURLConnection request data from the server, the code is as follows:

Try {
URL the URL=new URL (connectURL);
The connection url=(HttpURLConnection). The openConnection ();
Connection. SetDoOutput (true);//post cases need to be set to true, the default is false
Connection. SetDoInput (true);//Settings are from the httpURLConnection read in, the default is true
Connection. SetConnectTimeout (3000);
Connection. SetRequestMethod (" POST ");//set the connection type
Connection. The setRequestProperty (" the content-type ", "application/x - WWW - form - urlencoded");
//to access or write data on the server (using the input/output flow)
//connection for output flow
DataOutputStream outputStream=new DataOutputStream (connection. GetOutputStream ());
OutputStream. WriteBytes (" phone="+ URLEncoder. Encode (phone," utf-8 "));
OutputStream. WriteBytes (" password="+ URLEncoder. Encode (password," utf-8 "));
//don't know what's the problem with this code above, why post don't get the data?

OutputStream. Flush ();
OutputStream. Close ();//send out immediately shut down

The PHP code:

<? PHP
The header (" content-type: text/HTML; Charset=utf-8 ");

Include (' conn. PHP);//can normal connection, have test

//collection from the client of the incoming parameter
$phone=$_POST [" phone ");//the problem is can't get phone and password
$password=$_POST [" password ");


$SQL="SELECT * FROM value for user_login where phonenum='$phone' and password='$password";

$result=$conn - & gt; The query ($SQL);

If ($result - & gt; Num_rows & gt; 0 {
Echo (" success ");
}
The else {
Echo (" error ");
}

$conn - & gt; Close ();
?>

CodePudding user response:

Behind the test again, connected or not, ask about where great god is!
String body="phone=12345678901 & amp; Password=123 ";
DataOutputStream outputStream=new DataOutputStream (connection. GetOutputStream ());
OutputStream. WriteBytes (body);
OutputStream. Close ();
  • Related