I have manage to make work a login screen taking the java script from here
enter link description here
but my problem is that even if the login is wrong the new activity I am requesting opens.
Here is the code,
package com.xxxxxx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Admin extends Activity {
/** Called when the activity is first created. */
private Button login;
private EditText username, password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
login = (Button) findViewById(R.id.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String mUsername = username.getText().toString();
String mPassword = password.getText().toString();
tryLogin(mUsername, mPassword);
}
});
}
protected void tryLogin(String mUsername, String mPassword)
{
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "username="+mUsername+"&password="+mPassword;
try
{
url = new URL("http://xxxxxxxxxxxxxxx/login.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();
Intent intent = new Intent (Admin.this, Webview.class);
startActivity(intent);
finish();
}
catch(IOException e)
{
// Error
}
}
}
There is something wrong with the startactivity I guess but I cannot fix it.
Thanks in advance.
And here is the php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if($username=='xxxxxxx' && $password == 'xxxxxxxx'){
echo "true";
}
else{
echo "Login Failed";
}
?>
There doesn’t seem to be anything wrong with the startActivity call. startActivity has no idea if your login was correct or not, it is set to always fire so that’s what it does each and every time.
You probably want to check either the HTTP response code from your login attempt, or parse the output from the login attempt to see whether or not it was successful. Using either of those approaches, you can then choose to call startActivity if and only if the login attempt was successful.
Edit:
Based on your code, the String response should contain either “true” or “Login Failed”. Check the contents of response and only call startActivity if response is “true”, perhaps doing something else in the case of “Login Failed” to notify the user.