- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
PHP 文件:
<?php
require_once('include/db_functions.php');
$db = new DB_Functions();
// json response array
$response = array("error" => FALSE);
if (isset($_POST['email']) && isset($_POST['password'])) {
// receiving the post params
$email = $_POST['email'];
$password = $_POST['password'];
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// use is found
$response["error"] = FALSE;
$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"];
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
}
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters username or password is missing!";
}
echo json_encode($response);
?>
Java代码:
private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
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 {
int jsonStart = response.indexOf('{');
int jsonEnd = response.lastIndexOf('}');
if(jsonStart>=0 && jsonEnd>=0 && jsonEnd>jsonStart) response = response.substring(jsonStart,jsonEnd+1);
else response = "";
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;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
UPDATE 1: Logcat:
03-01 15:13:12.889 2099-2099/challenge.edison.harvest I/art: Not late-
enabling -Xcheck:jni (already on)
03-01 15:13:13.021 2099-2099/challenge.edison.harvest W/System: ClassLoader referenced unknown path: /data/app/challenge.edison.harvest-1/lib/x86
03-01 15:13:13.143 2099-2113/challenge.edison.harvest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-01 15:13:13.256 2099-2113/challenge.edison.harvest I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 15:13:13.386 2099-2113/challenge.edison.harvest W/EGL_emulation: eglSurfaceAttrib not implemented
03-01 15:13:13.386 2099-2113/challenge.edison.harvest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7345a0, error=EGL_SUCCESS
03-01 15:13:30.769 2099-2113/challenge.edison.harvest W/EGL_emulation: eglSurfaceAttrib not implemented
03-01 15:13:30.769 2099-2113/challenge.edison.harvest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad73a840, error=EGL_SUCCESS
03-01 15:13:33.005 2099-2105/challenge.edison.harvest W/art: Suspending all threads took: 779.254ms
03-01 15:13:33.006 2099-2099/challenge.edison.harvest I/Choreographer: Skipped 48 frames! The application may be doing too much work on its main thread.
03-01 15:13:34.301 2099-2105/challenge.edison.harvest W/art: Suspending all threads took: 629.777ms
03-01 15:13:34.302 2099-2099/challenge.edison.harvest I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread.
03-01 15:13:35.046 2099-2099/challenge.edison.harvest D/RegisterActivity: 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 />
<b>Fatal error</b>: Call to undefined method mysqli_stmt::get_result() in <b>/home/a6777446/public_html/include/db_functions.php</b> on line <b>64</b><br />
<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>
03-01 15:13:35.089 2099-2113/challenge.edison.harvest E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7510d0
03-01 15:13:35.096 2099-2099/challenge.edison.harvest W/System.err: org.json.JSONException: End of input at character 0 of
03-01 15:13:35.096 2099-2099/challenge.edison.harvest W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
03-01 15:13:35.097 2099-2099/challenge.edison.harvest W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
03-01 15:13:35.097 2099-2099/challenge.edison.harvest W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
03-01 15:13:35.097 2099-2099/challenge.edison.harvest W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
03-01 15:13:35.097 2099-2099/challenge.edison.harvest W/System.err: at challenge.edison.harvest.LoginActivity$3.onResponse(LoginActivity.java:128)
03-01 15:13:35.100 2099-2099/challenge.edison.harvest W/System.err: at challenge.edison.harvest.LoginActivity$3.onResponse(LoginActivity.java:115)
03-01 15:13:35.102 2099-2099/challenge.edison.harvest W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
03-01 15:13:35.103 2099-2099/challenge.edison.harvest W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
03-01 15:13:35.103 2099-2099/challenge.edison.harvest W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
03-01 15:13:35.103 2099-2099/challenge.edison.harvest W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
03-01 15:13:35.104 2099-2099/challenge.edison.harvest W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
03-01 15:13:35.104 2099-2099/challenge.edison.harvest W/System.err: at android.os.Looper.loop(Looper.java:148)
03-01 15:13:35.104 2099-2099/challenge.edison.harvest W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
03-01 15:13:35.104 2099-2099/challenge.edison.harvest W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-01 15:13:35.104 2099-2099/challenge.edison.harvest W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-01 15:13:35.104 2099-2099/challenge.edison.harvest W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-01 15:13:35.113 2099-2099/challenge.edison.harvest D/Volley: [1] Request.finish: 4449 ms: [ ] http://edisonharvest.comxa.com/login.php 0xafab10d0 NORMAL 1
03-01 15:13:35.242 2099-2113/challenge.edison.harvest W/EGL_emulation: eglSurfaceAttrib not implemented
03-01 15:13:35.242 2099-2113/challenge.edison.harvest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad75aae0, error=EGL_SUCCESS
03-01 15:13:38.626 2099-2113/challenge.edison.harvest E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7524f0
UPDATE 2: 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();
// verifying user password
$salt = $user['salt'];
$encrypted_password = $user['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
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;
}
}
?>
注册时数据存储在服务器上,但我在登录时收到此错误。好像我得到的是空的 json 对象。我正在使用 000webhost。我正在为一个重要的项目工作。请帮忙!!!谢谢!!
最佳答案
像下面这样更改函数 getUserByEmailAndPassword
。
将查询更改为以下内容或按原样进行并更改 $stmt->bind_result()
$stmt->bind_result($col1, $col2, $col3)
根据 SELECT *
查询检索到的列的方法。
$stmt = $this->conn->prepare("SELECT salt, encrypted_password FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
$stmt->bind_result($salt, $encrypted_password);
if($stmt->fetch()) {
// verifying user password
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
$user = array();
$user["salt"] = $salt;
$user["encrypted_password"] = $encrypted_password;
// if you want to return all the columns from database
// then add other lines here
// like $user["name"] = $name;
// and make sure you have declared $name in bind_result method
return $user;
}
} else {
return NULL;
}
} else {
return NULL;
}
也不要忘记对所有其他功能进行相对更改。
有关更多详细信息,请查看此 guid mysqli
希望对你有帮助。
关于java - 获取 json 错误 : end of input at character 0 of,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35718426/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!