一直在尝试这些代码,我想发送一封自动生成密码的电子邮件,然后随机密码将在我的数据库列中更新,即rich_password。
我真的不知道我的代码有什么问题。我是这个Android应用程序的新手,也使用PHP。我确实看到了一些与我有关的其他问题,但是,我仍然无法解决它。谁能帮帮我吗?在我的最后一年项目中,我真的需要帮助。
另一件事是在发送带有随机生成密码的电子邮件之后,随机生成的密码无法更新到我的数据库列中。
这是我的错误:
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);
}
}
?>
答案 0 :(得分:4)
正如错误消息所示:
[..snip..] json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
^^^
您尝试解析的JSON字符串无效 - 看起来它有一些原始HTML,这意味着它生成错误,或者PHP插入错误/警告并销毁字符串。
答案 1 :(得分:2)
我遇到了完全相同的问题 - 字符串在我的实际JSON对象之前生成了这样的表。我把这个表放到一个HTML文件中并显示出来 - 结果证明它是PHP函数的调用堆栈:
我通过在php文件中添加以下行来修复已弃用的警告:
error_reporting(E_ALL ^ E_DEPRECATED);
这修复了回复字符串,我现在能够成功解析它。