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,'."
".'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);
}
}
?>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…