我是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";
下面是logcat:
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 Parser如下:
`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);
}`
,DB功能是:
` 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测试了数据库连接,这很好。
答案 0 :(得分:0)
更改
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
作为List<NameValuePair> params = new ArrayList<NameValuePair>();