I am currently creating an Android Application
that is linked to a PHPMyAdmin
MYSQL
database on
Hosting24.com, this activity is intended to allow the user to sign up to enter details in an EditText box in the application that will be entered into the MYSQL database.
The code that I am attempting to run is giving a Toast
output of:
"Sorry, Username already chosen. Please choose another. "
eventhough the datbase is completely empty.
This seems to be because the JSON response
is false, my query is why would this be false?
Would it mean that the Access details
in the PHP script are incorrect?
Android Code:
public class SignUp extends Activity {
EditText UserName, Password;
Button btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_screen);
UserName = (EditText) findViewById(R.id.euUserName);
Password = (EditText) findViewById(R.id.euPass);
Password.setTransformationMethod(PasswordTransformationMethod.getInstance());
btnSignUp = (Button) findViewById(R.id.btnSingUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// get The User name and Password to sign up with
String userName = UserName.getText().toString();
String password = Password.getText().toString();
// Url to login PHP script on server
String serverURL = "http://r999hosting.com/UserRegistrationService.php?username="
+ userName + "&password=" + password;
new LongOperation().execute(serverURL);
}
});
}
/**
* Contains logic related to communication with PHP script over server
*
* @author
*
*/
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(SignUp.this);
String data = "";
int sizeData = 0;
protected void onPreExecute() {
Dialog.setMessage("Please wait.. ");
Dialog.show();
}
// Call after onPreExecute method
protected Void doInBackground(String... urls) {
// make POST call to web server
BufferedReader reader = null;
try {
// Define URL where to send data
URL url = new URL(urls[0]);
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line + "");
}
// Append Server Response To Content String
Content = sb.toString();
} catch (Exception ex) {
Error = ex.getMessage();
} finally {
try {
reader.close();
}
catch (Exception ex) {
}
}
return null;
}
protected void onPostExecute(Void unused) {
// Close progress dialog
Dialog.dismiss();
if (Error != null) {
} else {
// Start Parse Response JSON Data
String OutputData = "";
JSONObject jsonResponse;
try {
// Creates a new JSONObject with name/value mappings from
// the JSON string
jsonResponse = new JSONObject(Content);
String result = jsonResponse.get("result").toString();
if (result.equals("true")) {
// inform user they have signed up successfully
Toast.makeText(SignUp.this,
"Congrats: Sign Up Successfull",
Toast.LENGTH_LONG).show();
Intent i = new Intent(SignUp.this,
LoginHome.class);
startActivity(i);
// inform user of error signing up
Toast.makeText(getApplicationContext(),
"Congrats: Sign Up Successfull",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(
SignUp.this,
"Sorry, Username already chosen. Please choose another. ",
Toast.LENGTH_LONG).show();
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
PHP Script: (Note: real details not shown for security reasons)
<?php
if(isset($_GET['username']) && isset($_GET['password']))
{
$mysql_host = " ";
$mysql_database = " ";
$mysql_user = " ";
$mysql_password = " ";
// Provide host ip, mysql user name, password
$con = mysql_connect($mysql_host,$mysql_user,$mysql_password);
// Provide database name.
mysql_select_db($mysql_database);
$username=$_GET['username'];
$password=$_GET['password'];
$flag="false";
if(!empty($username) && !empty($password))
{
$sql="Insert into `Login` (`UserName`,`Password`) values ('$username','$password') ";
$result=mysql_query($sql);
if($result)
{
$count= mysql_affected_rows();
if($count > 0)
{
$flag="true"; //result true
}
}
mysql_close($con);
echo json_encode(array("result"=>$flag));
}
}
?>