在过去的两周里,我一直在开发我的慢跑应用程序,我试图建立一个使用PHP将信息存储到MYSQL中的登录/注册屏幕,我尝试过实施几十个PHP登录/注册屏幕,我没有运气,我的应用程序每次运行时崩溃,并尝试在登录前输入信息进行注册,我知道这是由于我的PHP脚本配置不正确,可以任何人帮我开发一个,非常感谢,这是我的代码。
public class signsignupview extends Activity {
private ImageButton settingHomeNav;
//------------------------------------------------------------------------------
// SharedPreferences to save user name and password
//------------------------------------------------------------------------------
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String namekey = "nameKey";
public static final String passkey = "passwordKey";
public SharedPreferences sharedpreferences;
//------------------------------------------------------------------------------
// Progress Dialog Bar
//------------------------------------------------------------------------------
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
//------------------------------------------------------------------------------
// Sign up fields
//------------------------------------------------------------------------------
EditText username2;
EditText password2;
EditText emailfill;
EditText addressfill;
//------------------------------------------------------------------------------
// URL to connect to PHP files for Sign up and Login
//------------------------------------------------------------------------------
private static String rootURL = "http://www.example.com/";
private static String url_sign_up = rootURL + "insert.php";
private static String url_sign = rootURL + "getlist.php";
//------------------------------------------------------------------------------
// Login fields"
//------------------------------------------------------------------------------
EditText emailsignin;
EditText password;
//------------------------------------------------------------------------------
// JSON node name
//------------------------------------------------------------------------------
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signsignup);
username2 = (EditText) findViewById(R.id.username2);
password2 = (EditText) findViewById(R.id.password2);
emailfill = (EditText) findViewById(R.id.emailfill);
addressfill = (EditText) findViewById(R.id.addressfill);
emailsignin = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
addListenerOnButton();
}
//------------------------------------------------------------------------------
// Data persistence
//------------------------------------------------------------------------------
@Override
protected void onResume() {
sharedpreferences=getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(namekey))
{
if(sharedpreferences.contains(passkey)){
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity(i);
}
}
super.onResume();
}
//------------------------------------------------------------------------------
// Home button
//------------------------------------------------------------------------------
public void addListenerOnButton() {
final Context context = this;
settingHomeNav = (ImageButton) findViewById(R.id.homeImageButton);
settingHomeNav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent2 = new Intent(context, Home.class);
startActivity(intent2);
}
});
//------------------------------------------------------------------------------
// Sign up button
//------------------------------------------------------------------------------
Button btn_signup = (Button) findViewById(R.id.signup);
btn_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//------------------------------------------------------------------------------
// Create a new function to run in background thread while Signing up
//------------------------------------------------------------------------------
new CreateNewProduct().execute();
}
});
//------------------------------------------------------------------------------
// Home button
//------------------------------------------------------------------------------
Button btn_signin = (Button) findViewById(R.id.login);
btn_signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//------------------------------------------------------------------------------
// Create a new function to run in background thread while Logging in
//------------------------------------------------------------------------------
new createSignIn().execute();
}
});
}
//------------------------------------------------------------------------------
// Background Async Task for user Log in
//------------------------------------------------------------------------------
class createSignIn extends AsyncTask<String, String, String > {
//------------------------------------------------------------------------------
// Before starting background thread Show Progress Dialog
//------------------------------------------------------------------------------
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(signsignupview.this);
pDialog.setMessage("Signing In ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
Editor editor = sharedpreferences.edit();
String email = emailsignin.getText().toString();
String passwd = password.getText().toString();
//------------------------------------------------------------------------------
// Building Parameters
//------------------------------------------------------------------------------
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", passwd));
//------------------------------------------------------------------------------
// Getting JSON Object
//------------------------------------------------------------------------------
JSONObject json = jsonParser.makeHttpRequest(url_sign,
"POST", params);
Log.d("Create Response", json.toString());
//------------------------------------------------------------------------------
// Check for success tag
//------------------------------------------------------------------------------
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
//------------------------------------------------------------------------------
// Login successful
//------------------------------------------------------------------------------
editor.putString(namekey, email);
editor.putString(passkey, passwd);
editor.commit();
//------------------------------------------------------------------------------
// Launching Home activity
//------------------------------------------------------------------------------
Intent i = new Intent(getApplicationContext(), Home.class);
startActivity(i);
finish();
} else {
//------------------------------------------------------------------------------
// Display Toast message - "Username/Password Incorrect !"
//------------------------------------------------------------------------------
Handler handler = new Handler(getApplicationContext().getMainLooper());
handler.post( new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Username/Password Incorrect !",Toast.LENGTH_LONG).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
//------------------------------------------------------------------------------
// After completing background task Dismiss the progress dialog
//------------------------------------------------------------------------------
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
//------------------------------------------------------------------------------
// Background Async Task for user Sign up
//------------------------------------------------------------------------------
class CreateNewProduct extends AsyncTask<String, String, String > {
//------------------------------------------------------------------------------
// Before starting background thread Show Progress Dialog
//------------------------------------------------------------------------------
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(signsignupview.this);
pDialog.setMessage("Signing Up..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
String name = username2.getText().toString();
String password = password2.getText().toString();
String email = emailfill.getText().toString();
String city = addressfill.getText().toString();
//------------------------------------------------------------------------------
// Building Parameters
//------------------------------------------------------------------------------
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("city", city));
//------------------------------------------------------------------------------
// Getting JSON Object
//------------------------------------------------------------------------------
JSONObject json = jsonParser.makeHttpRequest(url_sign_up,
"POST", params);
Log.d("Create Response", json.toString());
//------------------------------------------------------------------------------
// Check for success tag
//------------------------------------------------------------------------------
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
//------------------------------------------------------------------------------
// Display Toast message as success and prompt the user to log in
//------------------------------------------------------------------------------
Handler handler = new Handler(getApplicationContext().getMainLooper());
handler.post( new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "User Created Successfully. Please Login now",Toast.LENGTH_LONG).show();
}
});
} else {
//------------------------------------------------------------------------------
// Display Toast message as failure
//------------------------------------------------------------------------------
Handler handler = new Handler(getApplicationContext().getMainLooper());
handler.post( new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "Failed to create User.Please check entries",Toast.LENGTH_LONG).show();
}
});
Log.d("sign up ","failed");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
//------------------------------------------------------------------------------
// After completing background task Dismiss the progress dialog
//------------------------------------------------------------------------------
protected void onPostExecute(String file_url) {
pDialog.dismiss();
//------------------------------------------------------------------------------
// Funtion to clear the user sign up form
//------------------------------------------------------------------------------
ViewGroup group = (ViewGroup) findViewById(R.id.signsignupview);
clearForm(group);
}
}
//------------------------------------------------------------------------------
// Clear the user sign up form
//------------------------------------------------------------------------------
private void clearForm(ViewGroup group)
{
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearForm((ViewGroup)view);
}
}
}
用于登录的PHP脚本:
<?php
$connection = mysqli_connect("dbhost","username","password", "dbname") or die(mysqli_error($connection));
if(isset($_POST['tag']))
{
if($_POST['tag']=="success")
{
$email = mysqli_real_escape_string($connection,$_POST['email']);
$password = mysqli_real_escape_string($connection,$_POST['password']);
$strSQL = mysqli_query($connection,"select name from users where email='".$email."' and password='".md5($password)."'");
$Results = mysqli_fetch_array($strSQL);
if(count($Results)>=1)
{
$message = $Results['name']." Login Sucessfully!!";
}
else
{
$message = "Invalid email or password!!";
}
}
}
mysqli_close($connection);
?>
这是我得到的logcat错误消息:
04-15 18:46:48.923:E / WindowManager(5590):android.view.WindowLeaked:Activity com.runforlife.GPSRunner.signsignupview已泄露窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {最初添加的131e5078 VE .... R ...... D 0,0-1026,288} 04-15 18:46:48.923:E / WindowManager(5590):在android.view.ViewRootImpl。(ViewRootImpl.java:363) 04-15 18:46:48.923:E / WindowManager(5590):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261) 04-15 18:46:48.923:E / WindowManager(5590):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 04-15 18:46:48.923:E / WindowManager(5590):在android.app.Dialog.show(Dialog.java:298) 04-15 18:46:48.923:E / WindowManager(5590):at com.runforlife.GPSRunner.signsignupview $ createSignIn.onPreExecute(signsignupview.java:177) 04-15 18:46:48.923:E / WindowManager(5590):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 04-15 18:46:48.923:E / WindowManager(5590):在android.os.AsyncTask.execute(AsyncTask.java:535) 04-15 18:46:48.923:E / WindowManager(5590):at com.runforlife.GPSRunner.signsignupview $ 3.onClick(signsignupview.java:157) 04-15 18:46:48.923:E / WindowManager(5590):在android.view.View.performClick(View.java:4756) 04-15 18:46:48.923:E / WindowManager(5590):在android.view.View $ PerformClick.run(View.java:19748) 04-15 18:46:48.923:E / WindowManager(5590):在android.os.Handler.handleCallback(Handler.java:739) 04-15 18:46:48.923:E / WindowManager(5590):在android.os.Handler.dispatchMessage(Handler.java:95) 04-15 18:46:48.923:E / WindowManager(5590):在android.os.Looper.loop(Looper.java:135) 04-15 18:46:48.923:E / WindowManager(5590):在android.app.ActivityThread.main(ActivityThread.java:5256) 04-15 18:46:48.923:E / WindowManager(5590):at java.lang.reflect.Method.invoke(Native Method) 04-15 18:46:48.923:E / WindowManager(5590):at java.lang.reflect.Method.invoke(Method.java:372) 04-15 18:46:48.923:E / WindowManager(5590):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:898) 04-15 18:46:48.923:E / WindowManager(5590):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)