I am trying to establish a connection from my android application to a database made in MySQL. For that I am first trying to establish a connection from my app to a dummy database i created in phpMyAdmin because I don't want to corrupt my live data in my database. I am using a HttpURLConnection to post the form i created, below is my code.
public class Login1 extends Activity{
private EditText username=null;
private EditText password=null;
//private HttpClient httpclient=null;
//private HttpPost httppost =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
//httpclient = new DefaultHttpClient();
//httppost = new HttpPost("http://localhost/abc/index.php");
username = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
Button submit = (Button)findViewById(R.id.button2);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uname = username.getText().toString();
String pwd = password.getText().toString();
// TODO Auto-generated method stub
tryLogin(uname, pwd);
}
});
}
protected void tryLogin(String myusername, String mypassword)
{
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "myusername="+myusername+"&mypassword="+mypassword;
try
{
url = new URL("http://localhost/abc/index.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Toast.makeText(this,"Message from Server: \n"+ response, 0).show();
isr.close();
reader.close();
}
catch(IOException e)
{
// Error
}
}
}
The code for my php script is-
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect");
//mysqli_select_db("$db_name")or die("cannot select DB");
?>
<?php
include("dbpath.php");
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
//$myusername = stripslashes($myusername);
//$mypassword = stripslashes($mypassword);
//$myusername = mysql_real_escape_string($myusername);
//$mypassword = mysql_real_escape_string($mypassword);
//$sql="SELECT * FROM usertable WHERE username='$myusername' and password='$mypassword'";
$sql = "Insert into usertable (`username`,`password`) values ('$myusername', '$mypassword')";
$result = $con->query($sql);
//while($row = mysqli_fetch_array($result)) {
// echo "Success";
//}
echo "success";
?>
Now, when I run the debugger to check for bugs it gets stuck where i define
request = new OutputStreamWriter(connection.getOutputStream());
What is the reason for this thing?
0 comments:
Post a Comment