0
我剛剛開始使用Android Studio進行編碼,並且偶然發現了一個問題,我找不到解決方案。 我試圖創建一個登錄/註冊活動,並同時註冊工作完美的登錄是給我很難下一個錯誤:Android:如何將Java.String轉換爲JSONObject
"Json error :Value<br><table of type java.lang.String cannot be converted to JSONObject"
調試時,我設法找到問題和虐待粘貼的源代碼在這裏:
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
**JSONObject jObj = new JSONObject(response);**
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user.getString("created_at");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
調試器將我指向標有** ... **的行。
如果任何人都可以幫我解決這個問題,我將不勝感激。 (如果需要更多的代碼或類似的東西只是讓我知道,並添加它)。
btw我使用的是具有MYSQL版本的000webhost。 5.1。 數據庫連接是好的,它的工作(在註冊測試,它工作很好)。
我得到DDMS錯誤,
11-29 17:42:04.036: D/RegisterActivity(2188): Login Response: <br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><font face='Arial' size='1' color='#000000'><b>PHP Error Message</b></font></td></tr></table><br />
11-29 17:42:04.036: D/RegisterActivity(2188): <b>Fatal error</b>: Call to undefined method mysqli_stmt::get_result() in <b>/home/a4085630/public_html/include/DB_Functions.php</b> on line <b>59</b><br />
11-29 17:42:04.036: D/RegisterActivity(2188): <br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><div align='center'><a href='http://www.000webhost.com/'><font face='Arial' size='1' color='#000000'>Free Web Hosting</font></a></div></td></tr></table>
11-29 17:42:04.059: E/Surface(2188): getSlotFromBufferLocked: unknown buffer: 0xb3f967c0
11-29 17:42:04.065: W/System.err(2188): org.json.JSONException: Value <br><table of type java.lang.String cannot be converted to JSONObject
11-29 17:42:04.065: W/System.err(2188): at org.json.JSON.typeMismatch(JSON.java:111)
11-29 17:42:04.065: W/System.err(2188): at org.json.JSONObject.<init>(JSONObject.java:160)
11-29 17:42:04.065: W/System.err(2188): at org.json.JSONObject.<init>(JSONObject.java:173)
11-29 17:42:04.065: W/System.err(2188): at markitcommunity.org.markit.activity.LoginActivity$3.onResponse(LoginActivity.java:123)
11-29 17:42:04.065: W/System.err(2188): at markitcommunity.org.markit.activity.LoginActivity$3.onResponse(LoginActivity.java:114)
11-29 17:42:04.065: W/System.err(2188): at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
11-29 17:42:04.065: W/System.err(2188): at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
11-29 17:42:04.065: W/System.err(2188): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
11-29 17:42:04.065: W/System.err(2188): at android.os.Handler.handleCallback(Handler.java:739)
11-29 17:42:04.065: W/System.err(2188): at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 17:42:04.065: W/System.err(2188): at android.os.Looper.loop(Looper.java:148)
11-29 17:42:04.065: W/System.err(2188): at android.app.ActivityThread.main(ActivityThread.java:5417)
11-29 17:42:04.065: W/System.err(2188): at java.lang.reflect.Method.invoke(Native Method)
11-29 17:42:04.066: W/System.err(2188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-29 17:42:04.066: W/System.err(2188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-29 17:42:04.114: W/EGL_emulation(2188): eglSurfaceAttrib not implemented
11-29 17:42:04.114: W/OpenGLRenderer(2188): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2dc7a20, error=EGL_SUCCESS
DB_Functions.PHP碼 -
<?php
class DB_Functions {
private $conn;
// constructor
function __construct() {
require_once 'DB_Connect.php';
// connecting to database
$db = new Db_Connect();
$this->conn = $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
$stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
$stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
$result = $stmt->execute();
$stmt->close();
// check for successful store
if ($result) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return false;
}
}
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
return $user;
} else {
return NULL;
}
}
/**
* Check user is existed or not
*/
public function isUserExisted($email) {
$stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
// user existed
$stmt->close();
return true;
} else {
// user not existed
$stmt->close();
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;
}
}
?>
經過很長的研究幾乎無處不在,改變我的主人,我意識到,這個特定的行什麼給錯誤:
$user = $stmt->get_result()->fetch_assoc();
因爲它獲取表格的HTML版本我而不是其內容進入用戶。
任何人有任何想法如何解決這個請嗎?
有一些''
文字出現在你的'JSON'響應不能被轉換爲'JSONObject' – Wizard
它看起來像你的迴應,而不是JSON基於該錯誤 –
HTML作爲一個空間?它接收一個「電子郵件」和編碼64位密碼..哪裏可能是問題,我該如何克服它? –