不确定如何问这个问题,但我有一个Android应用程序,它使用PHP将用户注册/登录到我服务器上的mysql数据库中。使用WAMP在我的localhost上工作正常,但现在我得到了一个JSON异常。
这是我的Android LogCat:
01-21 01:55:17.054: E/JSON Parser(3688): Error parsing data org.json.JSONException: Unterminated comment at character 5 of
01-21 01:55:17.054: E/JSON Parser(3688): /*Success... Localhost via UNIX socket
01-21 01:55:17.054: E/JSON Parser(3688): {"tag":"login","success":1,"error":0,"uid":"52de1579866ce2.84736550","user":{"name":"test","email":"test@hotmail.com","created_at":"2014-01-20 23:36:41","updated_at":null}}
根据我的理解,问题是通过UNIX套接字的/*Success....Localhost,但我不知道如何删除这个....我的php文件没有添加这个,我不知道真的知道该怎么做。
有什么想法吗?
谢谢!
编辑:添加了PHP代码/文件
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'include/db_functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "success" => 0, "error" => 0);
// check for tag type
if ($tag == 'login') {
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password'];
// check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// user found
// echo json with success = 1
$response["success"] = 1;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
} else if ($tag == 'register') {
// Request type is Register new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
// check if user is already existed
if ($db->isUserExisted($email)) {
// user is already existed - error response
$response["error"] = 2;
$response["error_msg"] = "User already existed";
echo json_encode($response);
} else {
// store user
$user = $db->storeUser($name, $email, $password);
if ($user) {
// user stored successfully
$response["success"] = 1;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = 1;
$response["error_msg"] = "Error occured in Registartion";
echo json_encode($response);
}
}
} else {
echo "Invalid Request";
}
} else {
echo "Access Denied";
}
?>
<?php
class DB_Functions {
private $db;
//put your code here
// constructor
function __construct() {
require_once 'db_connect.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
}
// destructor
function __destruct() {
}
/**
* Storing new user
* returns user details
*/
public function storeUser($name, $email, $password) {
$uuid = uniqid('', true);
$hash = $this->hashSSHA($password);
$encrypted_password = $hash["encrypted"]; // encrypted password
$salt = $hash["salt"]; // salt
$result = mysql_query("INSERT INTO autoTracker_users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
// check for successful store
if ($result) {
// get user details
$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM autoTracker_users WHERE uid = $uid");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM autoTracker_users WHERE email = '$email'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$result = mysql_query("SELECT email from autoTracker_users WHERE email = '$email'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// user existed
return true;
} else {
// user not existed
return false;
}
}
/**
* Encrypting password
* @param password
* returns salt and encrypted password
*/
public function hashSSHA($password) {
$salt = sha1(rand());
$salt = substr($salt, 0, 10);
$encrypted = base64_encode(sha1($password . $salt, true) . $salt);
$hash = array("salt" => $salt, "encrypted" => $encrypted);
return $hash;
}
/**
* Decrypting password
* @param salt, password
* returns hash string
*/
public function checkhashSSHA($salt, $password) {
$hash = base64_encode(sha1($password . $salt, true) . $salt);
return $hash;
}
}
?>
<?php
class DB_Connect {
// constructor
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
// Connecting to database
public function connect() {
// connecting to mysql
$con = mysql_connect('servername', 'user', 'pass');
// selecting database
mysql_select_db('database');
// return database handler 69.195.124.151
return $con;
}
// Closing database connection
public function close() {
mysql_close();
}
}
?>
db_connect和db_functions在include文件夹中,因此我将它们称为&#39; include / db_functions.php&#39;在index.php里面。
当我注册用户时,它正确地将记录插入到我的数据库中,但是登录部分因为/ *成功行而失败。当我使用localhost而不是我的在线服务器时,它工作正常。谢谢你的帮助!
我正在使用自己的JSON解析器类,在使用WAMP和localhost时工作正常,当我在带有MySql db的bluehost网站上使用PHP时,我只会收到此错误。
public class JSONParser_Helpers {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser_Helpers() {
}
/**----------------------------
* Return JSON result from URL
* @param URL
*----------------------------- */
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//Log.e("Register URL Parser Class: ", url);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
//Log.e("HTTP Response: ", httpResponse.toString());
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
Log.e("JSON", json.toString());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我想出来但也许不是最好的方式......我刚刚将一个IF语句编辑到我的JSON Parser类中,这样如果该行以&#34; /&#34;比如造成我的问题的那个,现在它工作正常。如果您有任何更好的想法,请告诉我!
while ((line = reader.readLine()) != null) {
if (!line.startsWith("/")) {
sb.append(line + "\n");
//Log.e("LINE: ", line);
}
}
答案 0 :(得分:0)