Hi guy,
URL url = new URL(address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String user = params[0];
String pass = params[1];
String urlParams = "username=" + URLEncoder.encode(user, "UTF-8") +
"&password=" + URLEncoder.encode(pass, "UTF-8");
con.setRequestMethod("POST");
con.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(con.getOutputStream());
dos.writeBytes(urlParams);
dos.flush();
dos.close();
InputStream in = con.getInputStream();
StringBuilder str = new StringBuilder();
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
str.append(line);
str.append("\n");
}
br.close();
PHP code here
<pre>if(isset($_POST['username']) && isset($_POST['password']))
{
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("android");
$username = isset($_POST['username']) ? $_POST['username'] : "";
//$_POST['username'];
$password = isset($_POST['password']) ? $_POST['password'] : "";
//$_POST['password'];
$sqlSelect = "select username,hinh from nguoidung where username='$username'";
$users = mysql_query($sqlSelect);
if(mysql_num_rows($users) > 0)
{
echo json_encode(mysql_fetch_assoc($users));
$_SESSION['user'] = "ok";
}else{
echo json_encode(array("loi"=>"user khong hop le"));
}else
{
echo json_encode(array("loi"=>"user khong hop le"));
}
}
Can view
here