I'm looking for an example of a login and register android application using php and a remote database that really works .Can you help me please ?
-
3Hi! Welcome to StackOverflow! StackOverflow is for programming questions, and this is not a valid programming question. If you work on writing the app yourself, you are welcome to use StackOverflow for specific questions about specific problems you are encountering with specific code that you have written.CommonsWare– CommonsWare2013-03-24 22:31:53 +00:00Commented Mar 24, 2013 at 22:31
-
Are you planning to use android webview?Red Cricket– Red Cricket2013-03-24 22:32:54 +00:00Commented Mar 24, 2013 at 22:32
-
OK? I actually had a problem with my own code: all of the http networking is going to be done using the connect button: to make things work , i must place the networking code insode an asynchtask but idon't know how to place this class inside my click() method.adel oueslati– adel oueslati2013-03-24 22:40:22 +00:00Commented Mar 24, 2013 at 22:40
-
Red Cricket No , i'm not planning to use an android web view , i'm using regular activityadel oueslati– adel oueslati2013-03-24 22:43:33 +00:00Commented Mar 24, 2013 at 22:43
-
1See this link LinkAwadKab– AwadKab2013-03-24 23:42:19 +00:00Commented Mar 24, 2013 at 23:42
1 Answer
Mkay... I like to believe that you are not lazy. Might be my insomnia playing tricks on me.
Starting with this hypothesis I'll give you an example consisting of a LoginActivity and a LoginValidator. Please use them as examples and adapt them to your needs. Although they DO work you might need to tweak here and there to make it fit in your current context.
Also, at the bottom of my answer you'll find a list with Useful Links.
LoginActivity.java
import java.util.concurrent.ExecutionException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.example.android.yourappname.R;
import com.example.android.yourappname.LoginValidator;
import com.example.android.yourappname.SavedValues; // key value pairs... used for saving the state of the Activity
public class LoginActivity extends Activity {
public EditText usr;
public EditText pass;
final Context context = this;
private TextView loginButton;
private Boolean justCreated = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
initLoginButton();
initEditTexts();
addSwipeGesture();
justCreated = true;
}
@Override
public void onPause()
{
super.onPause();
Log.v("Login", "onPause");
SavedValues.setLoginUser(usr.getText().toString());
SavedValues.setLoginPassword(pass.getText().toString());
justCreated = false;
}
@Override
public void onResume()
{
super.onResume();
Log.v("Login", "onResume");
if (justCreated == true) //if the user recreated the activity, restore the login values from the previous instance
{
usr.setText(SavedValues.getLoginUser(), TextView.BufferType.EDITABLE);
pass.setText(SavedValues.getLoginPassword(), TextView.BufferType.EDITABLE);
usr.setSelection(usr.getText().length());
}
else //if the user only left the activity clear the login values
{
usr.setText("", TextView.BufferType.EDITABLE);
pass.setText("", TextView.BufferType.EDITABLE);
}
}
private void initLoginButton()
{
loginButton = (TextView) findViewById(R.id.btnLogin);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onLoginClick();
}
});
deactivateLoginButton();//the login button will be activated after the user types something in the user name field
}
private void initEditTexts()
{
usr = (EditText) findViewById(R.id.usr);
pass = (EditText) findViewById(R.id.pass);
//set the focus on the user's editText
usr.setFocusable(true);
usr.setFocusableInTouchMode(true);
usr.requestFocus();
usr.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
if(usr.length() == 0)
{
deactivateLoginButton();
}
else
{
activateLoginButton();
}
}
});
//set the login values from saved data
usr.setText(SavedValues.getLoginUser(), TextView.BufferType.EDITABLE);
pass.setText(SavedValues.getLoginPassword(), TextView.BufferType.EDITABLE);
}
private void addSwipeGesture()
{
View root = findViewById(android.R.id.content).getRootView();
root.setOnTouchListener(new OnSwipeTouchListener(){
public boolean onSwipeLeft() {
onLoginClick();
return true;
}
});
}
private void deactivateLoginButton()
{
loginButton.setVisibility(View.INVISIBLE);
loginButton.setClickable(false);
}
private void activateLoginButton()
{
if (loginButton.getVisibility() != View.VISIBLE)
{
loginButton.setVisibility(View.VISIBLE);
loginButton.setClickable(true);
}
}
private void onLoginClick()
{
// Attempting Login
// test U + P + MAC
/*
* // testing MAC address WifiManager manager = (WifiManager)
* getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo =
* manager.getConnectionInfo(); String MACAddress =
* wifiInfo.getMacAddress();
*/
String user = usr.getText().toString();
String password = pass.getText().toString();
if (!isOnline()) {
alert("Must be connected to network!");
} else {
LoginValidator logval = new LoginValidator();
Boolean validity = false;
try {
validity = (Boolean) logval.execute(user, password).get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (validity) {
Intent i = new Intent(getApplicationContext(), GMC_MainActivity.class);
startActivity(i);
} else {
alert("Invalid username or password");
}
}
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting())
{
return true;
}
return false;
}
public void alert(String s) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(s);
alertDialogBuilder.setCancelable(false).setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close the
// dialog
// box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
LoginValidator.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import android.os.AsyncTask;
public class LoginValidator extends AsyncTask<String, Void, Boolean> {
Connection conn = null;
private String jdbcURL = ""; // URL for your database... IP, port etc.
private String user = ""; // Username for your DB
private String passwd = ""; // Password for your DB
@Override
protected Boolean doInBackground(String... arg0) {
String username = arg0[0];
String password = arg0[1];
Boolean v = false;
ResultSet rs = null;
Statement stmt = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); // I use an oracle DB addapt this to your own DB
conn = DriverManager.getConnection(jdbcURL, user, passwd);
try {
stmt = conn.createStatement();
try {
rs = stmt
.executeQuery("SELECT * FROM ts_v4_res.user_t WHERE user_name='"
+ username + "'" /*+" AND pass_word='" +password+"'"*/);
if (rs.next()) {
String p=rs.getString("pass_word");
if (p==null){
if(password.equals(""))
v = true;}
else
if(p.equals(password))
v=true;
}
} finally {
try {
rs.close();
} catch (Throwable ignore) {
}
}
} finally {
try {
stmt.close();
} catch (Throwable ignore) {
}
}
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.lang.ClassCastException e) {
e.printStackTrace();
}
return v;
}
}
Useful links with examples(plenty of them out there):
Android Login activity with MySQL database connection
Android login activity using mysql database
How to connect login to my android app from a mysql database
Android Login activity with MySQL database connection
Hope this will get you started.
Cheers