1

I'm getting this when I click my login button and I have no idea why, I don't know why it says Hello in the error and I searched my whole project and can't find the occurrence of "Hello". Is there another cause for this error?

public class Login extends Activity implements OnClickListener {

    private EditText user, pass;
    private Button mSubmit, mRegister;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();




    private static final String LOGIN_URL = "http://xxxxxxxxxxxx";



    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // setup input fields
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        // setup buttons
        mSubmit = (Button) findViewById(R.id.login);
        mRegister = (Button) findViewById(R.id.register);

        // register listeners
        mSubmit.setOnClickListener(this);
        mRegister.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.login:
                new AttemptLogin().execute();
                break;
            case R.id.register:
                Intent i = new Intent(this, Register.class);
                startActivity(i);
                break;

            default:
                break;
        }
    }

    class AttemptLogin extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                        params);

                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());
                    // save user data
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(Login.this);
                    Editor edit = sp.edit();
                    edit.putString("username", username);
                    edit.commit();

                    Intent i = new Intent(Login.this, MainActivity.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
            }

        }

    }

}

Edited code

<?php
       if (!isset($_SESSION)) {
  session_start();
}


//load and connect to MySQL database stuff
require("config.inc.php");




if (!empty($_POST)) {
$query = "SELECT id,password, username, iv FROM users WHERE username = :username";




    $username = $_POST['username'];

    //encrypting the username and password to compare to the ones stored in the databaase



 $query_params = array(
        ':username' => $username,
    );

    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);


    }
    catch (PDOException $ex) {

        $response["success"] = 0;
        $response["message"] = "Database Error1. Please Try Again!";
        die("Failed to run query: " . $ex->getMessage());

    }




    //fetching all the rows from the query

        $password = $_POST['password'];

    $row = $stmt->fetch();
    $key = $row['iv'];
    $encrypted_password = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $password, MCRYPT_MODE_CBC, $key); 
 if ($row) {
        if ($encrypted_password == $row['password']) {


            $login_ok = true;
        }
    }

    if ($login_ok) {
        $response["success"] = 1;
        $response["message"] = "Login successful!";
        die(json_encode($response));
       echo json_encode($response);



    } else {
        $response["success"] = 0;
        $response["message"] = "Invalid Credentials!";
        die(json_encode($response));
    }

} else {
?>
                <h1>Login</h1>
                <form action="login.php" method="post">
                    Username:<br />
                    <input type="text" name="username" placeholder="username" />
                    <br /><br />
                    Password:<br />
                    <input type="password" name="password" placeholder="password" value="" />
                    <br /><br />
                    <input type="submit" value="Login" />
                </form>
                <a href="register.php">Register</a>
        <?php
}

?>

1 Answer 1

1

May be something wrong with your JSONParser class or response from the server might be returning "hello". Could you post that class as well to make things more clearer.

Sign up to request clarification or add additional context in comments.

13 Comments

I found the hello string, it was a small echo that I deleted. I am getting this now, "Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 04"
The JSON response is {"success":1,"message":"Login successful!"}
I think you are not getting proper response from your server. Check if the server side is returning a proper result for your login query.
Is this not it? {"success":1,"message":"Login successful!"}
Yes it is. Are you still getting the parsing exception? Could you post the latest exception log?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.