3

一直在尝试这些代码,我想发送一封电子邮件,它将自动生成密码,然后随机密码将在我的数据库列中更新,即 encrypted_pa​​ssword。

我真的不知道我的代码有什么问题。我是这个 android 应用程序的新手,也使用 php.ini。我确实看到了一些与我有关的其他问题,但是,我仍然无法解决它。谁能帮帮我吗?对于我最后一年的项目,我真的需要这方面的帮助。

另一件事也是在发送带有随机生成密码的电子邮件后,随机生成的密码无法更新到我的数据库列中。

这是我的错误:

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)

我的安卓代码:

 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();
    }
}

 }

和我的 php 文件:

<?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: noreply@pivestigator.netne.net';

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);
}
 }
?>

正如错误消息所说:

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

您尝试解析的 JSON 字符串无效 - 看起来它有一些原始 HTML,这意味着它要么生成错误,要么 PHP 正在插入错误/警告并破坏字符串。

4

2 回答 2

4

正如错误消息所说:

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

您尝试解析的 JSON 字符串无效 - 看起来它有一些原始 HTML,这意味着它要么生成错误,要么 PHP 正在插入错误/警告并破坏字符串。

于 2012-08-29T03:43:40.940 回答
2

我遇到了完全相同的问题——字符串在我的实际 JSON 对象之前生成了一个这样的表。我将此表放入一个 HTML 文件并显示它 - 原来它是 PHP 函数的调用堆栈:在此处输入图像描述

我通过在 php 文件中添加以下行来修复不推荐使用的警告:

error_reporting(E_ALL ^ E_DEPRECATED);

这修复了回复字符串,我现在能够成功解析它。

于 2014-12-26T11:04:02.227 回答