我认为我已经配置了我的PHP脚本,用于通过PHP将我的Android连接到MySQL,基本上我正在尝试开发一个运行的应用程序,通过GPS跟踪距离和移动,并可以将信息存储到SQL数据库。
我已经实现了一个登录/注册类,配置文件类和排行榜,我想存储SQL的信息,但我无法通过我的登录/注册屏幕。当我尝试输入要注册的信息,因为我的应用程序需要登录使用,我的应用程序就崩溃了。
我认为这与我的PHP脚本不太确定。这是我的登录/注册类:
//------------------------------------------------------------------------------
// Class implementation: signsignupview
// Login and Signup functionality
//------------------------------------------------------------------------------
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 url_sign_up = "http://mayar.abertay.ac.uk/~1205418/sign_up.php";
private static String url_sign = "http://mayar.abertay.ac.uk/~1205418/signin.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("host","username","password", "DBname") or die(mysqli_error($connection));
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
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);
?>
PHP注册脚本:
<?php
$connection = mysqli_connect("host","username","password", "DB name") 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);
?>