3

Have been trying on these codes and I want to send a email which will auto generate the password and the random password will then update in my database column which is the encrypted_password.

I really dont know whats wrong with my code. I am new to this android application and also using the php. And i did see some other questions that are link to mine, however, I still cant solve it. Can anyone please help me? I really need help on this for my final year project.

Another thing is also after sending the email with the random generated password, the random generated password cannot be updated into my database column.

This is my error:

08-29 10:44:10.976: E/JSON Parser(1594): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
08-29 10:44:10.987: W/dalvikvm(1594): threadid=10: thread exiting with uncaught exception (group=0x40014760)
08-29 10:44:11.006: E/AndroidRuntime(1594): FATAL EXCEPTION: AsyncTask #1
08-29 10:44:11.006: E/AndroidRuntime(1594): java.lang.RuntimeException: An error occured while executing doInBackground()
08-29 10:44:11.006: E/AndroidRuntime(1594):     at android.os.AsyncTask$3.done(AsyncTask.java:266)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.lang.Thread.run(Thread.java:1020)
08-29 10:44:11.006: E/AndroidRuntime(1594): Caused by: java.lang.NullPointerException
08-29 10:44:11.006: E/AndroidRuntime(1594):     at com.pivestigator.login.ForgetPasswordActivity$SendEmail.doInBackground(ForgetPasswordActivity.java:93)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at com.pivestigator.login.ForgetPasswordActivity$SendEmail.doInBackground(ForgetPasswordActivity.java:1)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at android.os.AsyncTask$2.call(AsyncTask.java:252)
08-29 10:44:11.006: E/AndroidRuntime(1594):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

my android code:

 public class ForgetPasswordActivity extends Activity {

EditText textforgetEmail;
TextView email_error;

 // Progress Dialog
private ProgressDialog pDialog;

 private static String url_email = "...";

// JSON Node names
        private static final String TAG_SUCCESS = "success";

    JSONParser jsonParser = new JSONParser();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.forget_password);

    Button buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    textforgetEmail = (EditText) findViewById(R.id.forgetEmail);
    email_error = (TextView) findViewById(R.id.email_error);

    // Link to Login Screen
    buttonSubmit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // creating new product in background thread
                new SendEmail().execute();
            }
    });

}

class SendEmail extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ForgetPasswordActivity.this);
       // pDialog.setMessage("Sending Email..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
       // pDialog.show();
    }

    protected String doInBackground(String... args) {
        runOnUiThread(new Runnable() {
            public void run() {
                // SENDING EMAIL METHOD HERE
                String sendemail = textforgetEmail.getText().toString();

                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("email", sendemail));

                // getting JSON Object
                // Note that forgotPassword URL accepts POST method
                JSONObject json = jsonParser.makeHttpRequest(
                        url_email, "POST", params);

                // check log cat for response
                 //Log.d("Sending email Response", json.toString());

                // check for success tag
                try {
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        // successfully email sent
                        Intent intent = new Intent(getApplicationContext(),
                                ForgetPasswordConfirmActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        // failed to send email
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }
}

 }

and my php file:

<?php

$response = array();

$random = str_rand();

 function str_rand($length = 8, $seeds = 'alphanum')
{
// Possible seeds
$seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
$seedings['numeric'] = '0123456789';
$seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
$seedings['hexidec'] = '0123456789abcdef';

// Choose seed
if (isset($seedings[$seeds]))
{
    $seeds = $seedings[$seeds];
}

// Seed generator
list($usec, $sec) = explode(' ', microtime());
$seed = (float) $sec + ((float) $usec * 100000);
mt_srand($seed);

// Generate
$str = '';
$seeds_count = strlen($seeds);

for ($i = 0; $length > $i; $i++)
{
    $str .= $seeds{mt_rand(0, $seeds_count - 1)};
}

return $str;
}

// check for required fields
if (isset($_POST['email'])) {

$email = $_POST['email'];


$to = $email;
$subject = 'This is an email.';
$body = 'Hi,'."\n\n".'This is your new password: '."$random".'test';
$headers = 'From: [email protected]';

if (mail($to, $subject, $body, $headers)) {
// include db connect class
require_once __DIR__ . '/DB_Connect.php';

// connecting to db
$db = new DB_CONNECT();

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Email successfully sent.";

    // echoing JSON response
    echo json_encode($response);
} 

}
else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
 }
?>
1
  • tip: microtime(true) will return a normal float value, saving you all the explode+math manipulations you're doing. Commented Aug 29, 2012 at 3:54

2 Answers 2

4

As the error message says:

[..snip..] json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
                                     ^^^

The JSON string you're trying to parse is invalid - looks like it's got some raw HTML, which mean it's either badly generated, or PHP is inserting errors/warnings and destroying the string.

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

3 Comments

Most likely the very beginning of the string, but unless you capture the string and view it directly BEFORE trying to parse it as json, no way to tell for sure.
i see.. tried a lot of ways already.. but still.. no progress.
EVEN i am getting the same error in my logcat when i am inserting into database but i tried lot like changing all the print statement to echo it didnt change @Twister
2

I had the exact same problem - the string had a table like this generated before my actual JSON object. I put this table into a HTML file and displayed it - turns out it's a call stack for PHP functions:enter image description here

I fixed the deprecated warnings by adding the following line in the php file:

error_reporting(E_ALL ^ E_DEPRECATED);

This fixed the reply string and I was now able to parse it successfully.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.