0
我有一個註冊表單,我在進行驗證。 用戶切換關注電子郵件字段後,我想檢查是否在數據庫中註冊了此類電子郵件,並對其進行敬酒。 不管我做什麼,不過,我總是得到線585空指針異常是以下行:JSON對象返回null(android + json + php + mysql)
Log.d("Checking wether this email exists in database", json.toString());
這裏是整個事情:
class CheckWetherAUserWithSuchEmailExists extends AsyncTask<String, String, String> {
int success;
@Override
protected String doInBackground(String... args) {
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
Log.d("Kylie", userEmail);
params.add(new BasicNameValuePair("user_email", userEmail));
Log.d("request!", "starting");
// Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(CHECK_EMAIL_EXISTS_URL, "POST", params);
// full json response
Log.d("Checking wether this email exists in database", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Email exists", json.toString());
finish(); //fire up an alarm
userWithTheSameEmail = true;
return json.getString(TAG_MESSAGE);
} else if(success == 2) {
Log.d("Email doesnt exist", json.getString(TAG_MESSAGE));
userWithTheSameEmail = false;
return json.getString(TAG_MESSAGE);
} else {
Log.d("Something happened", json.getString(TAG_MESSAGE));
userWithTheSameEmail = false;
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
這裏是PHP腳本接受的JSON:
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
//gets user's info based off of a username.
$user_email = mysql_real_escape_string($_POST['user_email'];
#$user_email_to_query = "\"". $user_email . "\"";
$query = "
SELECT
user_email
FROM users
WHERE
user_email = '$user_email'
";
if (mysql_num_rows($row) > 0) {
$response["success"] = 1;
$response["message"] = "User with such email exists";
} else {
$response["success"] = 2;
$response["message"] = "User with such email doesnt exist. You can continue registering";
}
} else {
die("Another brick in the wall");
}
下面是配置:
<?php
$username = "asd_asd";
$password = "123123";
$host = "localhost";
$dbname = "asd_asd";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
header('Content-Type: text/html; charset=utf-8');
session_start();
這是表:
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL,
`user_email` varchar(50) NOT NULL,
`user_password` varchar(100) NOT NULL,
`user_sex` varchar(10) NOT NULL,
`user_avatar` varchar(100) DEFAULT NULL,
`user_date_registered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_last_seen` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_points` int(11) NOT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_name` (`user_name`,`user_email`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
這是錯誤的堆棧跟蹤:
D/Kylie(4458): [email protected]
D/request!(4458): starting
Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
threadid=9: thread exiting with uncaught exception (group=0x4001e578)
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.NullPointerException
net.asdasd.activities.Signup$CheckWetherAUserWithSuchEmailExists.doInBackground(Signup.java:585)
at net.asdasd.activities.Signup$CheckWetherAUserWithSuchEmailExists.doInBackground(Signup.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
... 4 more
如果有任何我應該提供更多信息,身份證很樂意這樣做。
是的,你是對的:))我從來沒有真正學過PHP,現在我掙扎。謝謝! –