I'm following this tutorial: http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ Copy-pasted the php files, only changed the database parameters and the amount of data user table stores. Whenever I try to register a user, I get this in my logcat:
03-31 14:07:04.694 11972-12241/com.example.giedrius.keliokliuciuregistravimosistema E/Volley: [192] BasicNetwork.performRequest: Unexpected response code 500 for http://giekri.stud.if.ktu.lt/register.php
03-31 14:07:04.746 11972-11972/com.example.giedrius.keliokliuciuregistravimosistema E/RegistrationScreen: Registration Error: null
My AppConfig:
package com.example.giedrius.keliokliuciuregistravimosistema;
public class AppConfig {
// Server user login url
public static String URL_LOGIN = "http://giekri.stud.if.ktu.lt/login.php";
// Server user register url
public static String URL_REGISTER = "http://giekri.stud.if.ktu.lt/register.php";
}
AppController unchanged:
package com.example.giedrius.keliokliuciuregistravimosistema;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
And Registration file:
package com.example.giedrius.keliokliuciuregistravimosistema;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import com.example.giedrius.keliokliuciuregistravimosistema.R;
import com.example.giedrius.keliokliuciuregistravimosistema.AppConfig;
import com.example.giedrius.keliokliuciuregistravimosistema.AppController;
import com.example.giedrius.keliokliuciuregistravimosistema.SQLiteHandler;
import com.example.giedrius.keliokliuciuregistravimosistema.SessionManager;
public class RegistrationScreen extends Activity {
private static final String TAG = RegistrationScreen.class.getSimpleName();
private Button btnRegister;
private Button btnLinkToLogin;
private EditText inputFullName;
private EditText inputFullSurname;
private EditText inputAddress;
private EditText inputEmail;
private EditText inputPassword;
private EditText inputPassword2;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration_screen);
inputFullName = (EditText) findViewById(R.id.et_Vardas);
inputFullSurname = (EditText) findViewById(R.id.et_Pavarde);
inputAddress = (EditText) findViewById(R.id.et_Adresas);
inputEmail = (EditText) findViewById(R.id.et_elPastas);
inputPassword = (EditText) findViewById(R.id.et_Slaptazodis);
inputPassword2 = (EditText) findViewById(R.id.et_Slaptazodis2);
btnRegister = (Button) findViewById(R.id.bt_Registruotis);
btnLinkToLogin = (Button) findViewById(R.id.bt_Prisijungti);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(RegistrationScreen.this,
MainMenu.class);
startActivity(intent);
finish();
}
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString().trim();
String surname = inputFullName.getText().toString().trim();
String address = inputFullName.getText().toString().trim();
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
String password2 = inputPassword2.getText().toString().trim();
if (!name.isEmpty() && !surname.isEmpty() && !address.isEmpty() && !email.isEmpty() && !password.isEmpty() && !password2.isEmpty()) {
registerUser(name, surname, address, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details correctly!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginScreen.class);
startActivity(i);
finish();
}
});
}
/**
* Function to store user in MySQL database will post params(tag, name,
* email, password) to register url
* */
private void registerUser(final String name, final String surname, final String address, final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST, AppConfig.URL_REGISTER, new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String surname = user.getString("surname");
String address = user.getString("address");
String email = user.getString("email");
// Inserting row in users table
db.addUser(name, surname, address, email);
Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(
RegistrationScreen.this,
LoginScreen.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("surname", surname);
params.put("address", address);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
I have tried to debug it and it looks to get messed up in this part:
StringRequest strReq = new StringRequest(Method.POST, AppConfig.URL_REGISTER, new Response.Listener<String>()
it jumps through everything in the curly brackets right to
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
Does anyone have any idea what's going on? I can provide the rest of the files if needed.
EDIT Addint Registration php file:
<?php
require_once 'DB_Functions.php';
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['name']) && isset($_POST['surname']) && isset($_POST['address']) && isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$name = $_POST['name'];
$surname = $_POST['surname'];
$address = $_POST['address'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user with the same email already exists
if ($db->doesUserExist($email)) {
// user already exists
$response["error"] = TRUE;
$response["error_msg"] = "User already exists with email: " . $email;
echo json_encode($response);
} else {
// create a new user
$user = $db->storeUser($name, $surname, $address, $email, $password);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["user"]["name"] = $user["name"];
$response["user"]["surname"] = $user["surname"];
$response["user"]["address"] = $user["address"];
$response["user"]["email"] = $user["email"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (name, surname, address, email or password) is missing!";
echo json_encode($response);
}
?>
P.S. The URLs should be working, because I used them in another login tutorial and it worked fine. But it had only the login part, so I moved to this tutorial which also has registration. And now it doesn't work. The tutorial which worked is this