gpt4 book ai didi

android:通过 php 连接 mysql(WAMP 设置)- 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 22:56:18 24 4
gpt4 key购买 nike

我是 android 的新手,所以任何建议都会非常有帮助。

我正在尝试通过 PHP 连接 Mysql,但应用程序在尝试连接时崩溃了。

    private class ProcessLogin extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
String email,password;
@Override
protected void onPreExecute() {
super.onPreExecute();
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.pword);
email = inputEmail.getText().toString();
password = inputPassword.getText().toString();
pDialog = new ProgressDialog(login.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Logging in ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
return json;
}

DB_Config 文件是这样的:

    define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "aap1"); // database name
define('DB_SERVER', "localhost"); // db server

我在数据库中添加了表条目。

此外,函数调用如下:

    public JSONObject loginUser(String email, String password){
// Building Parameters
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("tag", Custlogin_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
return json;
}

private static String loginURL = "http://127.0.0.1/app1/index.php";

下面是日志:

    10-07 23:22:40.596: E/Buffer Error(2003): Error converting result java.lang.NullPointerException: lock == null
10-07 23:22:40.676: E/JSON Parser(2003): Error parsing data org.json.JSONException: End of input at character 0 of
10-07 23:22:40.766: E/AndroidRuntime(2003): FATAL EXCEPTION: main
10-07 23:22:40.766: E/AndroidRuntime(2003): Process: com.example.app1, PID: 2003
10-07 23:22:40.766: E/AndroidRuntime(2003): java.lang.NullPointerException
10-07 23:22:40.766: E/AndroidRuntime(2003): at com.example.app1.login$ProcessLogin.onPostExecute(login.java:180)
10-07 23:22:40.766: E/AndroidRuntime(2003): at com.example.aap1.login$ProcessLogin.onPostExecute(login.java:1)
10-07 23:22:40.766: E/AndroidRuntime(2003): at android.os.AsyncTask.finish(AsyncTask.java:632)
10-07 23:22:40.766: E/AndroidRuntime(2003): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-07 23:22:40.766: E/AndroidRuntime(2003): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-07 23:22:40.766: E/AndroidRuntime(2003): at android.os.Handler.dispatchMessage(Handler.java:102)
10-07 23:22:40.766: E/AndroidRuntime(2003): at android.os.Looper.loop(Looper.java:136)
10-07 23:22:40.766: E/AndroidRuntime(2003): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-07 23:22:40.766: E/AndroidRuntime(2003): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 23:22:40.766: E/AndroidRuntime(2003): at java.lang.reflect.Method.invoke(Method.java:515)
10-07 23:22:40.766: E/AndroidRuntime(2003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-07 23:22:40.766: E/AndroidRuntime(2003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-07 23:22:40.766: E/AndroidRuntime(2003): at dalvik.system.NativeStart.main(Native Method)

JSON解析器如下:

`public JSONObject getJSONFromUrl(String url, List<?> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity
((List<? extends NameValuePair>) params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}`

PHP 脚本是:

`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["user"]["email"] = $user["email"];
$response["user"]["uname"] = $user["username"];
$response["user"]["uid"] = $user["unique_id"];
$response["user"]["created_at"] = $user["created_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);
}`

数据库函数是:

` public function getUserByEmailAndPassword($email, $password) {
        $result = mysqli_query("SELECT * FROM users WHERE email = '$email'")
or die(mysqli_error());
        // check for result
        $no_of_rows = mysqli_num_rows($result);
        if ($no_of_rows > 0) {
            $result = mysqli_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;
        }
   }`

我已经通过 testmysql.php 测试了数据库连接,没有问题。

最佳答案

改变

 List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

作为List<NameValuePair> params = new ArrayList<NameValuePair>();

关于android:通过 php 连接 mysql(WAMP 设置)- 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26248883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com