安装wamp用本地的mysql数据库_使用WAMP作为本地服务器无法将Android应用程序连接到MySQL数据库...

博主在尝试使用WAMP作为本地服务器,让Android应用程序连接到MySQL数据库进行登录验证。在登录.inc.php文件中设置数据库连接,但遇到了PDO构造函数的错误。Logcat显示连接超时问题,尽管可以通过手机浏览器访问本地服务器。
摘要由CSDN通过智能技术生成

我正在尝试创建一个简单的登录到我的Android应用程序,该应用程序根据这些教程将用户的输入与数据库中的数据进行比较:

我正在使用WAMP作为本地服务器来测试我是否可以 Build 连接 . 我有两个名为config.inc.php和login.inc.php的PHP文件放在以下目录中:C:\ wamp64 \ www \ DUFT它们看起来像这样:

config.inc.php文件

$username = "root";

$password = "root";

$dbname = "duft";

try {

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}

catch(PDOException $e)

{

die("OOPs something went wrong");

}

?>

login.inc.php

include 'config.inc.php';

// Check whether username or password is set from android

if(isset($_POST['username']) && isset($_POST['password']))

{

// Innitialize Variable

$result='';

$username = $_POST['username'];

$password = $_POST['password'];

// Query database for row exist or not

$sql = 'SELECT * FROM tbl_login WHERE email = :username AND adgangskode = :password';

$stmt = $conn->prepare($sql);

$stmt->bindParam(':username', $username, PDO::PARAM_STR);

$stmt->bindParam(':password', $password, PDO::PARAM_STR);

$stmt->execute();

if($stmt->rowCount())

{

$result="true";

}

elseif(!$stmt->rowCount())

{

$result="false";

}

// send result back to android

echo $result;

}?>

然后我有我的LoginActivity类,我正在使用AsyncTask在后台 Build 与数据库的连接 . 在onPostExecute()方法中,如果用户输入与数据库中的内容匹配,我正在尝试启动一个新活动 . 但我一直从PHP文件中得到这个错误:

(!)警告:PDO :: __ construct():MySQL服务器在第10行的C:\ wamp64 \ www \ DUFT \ login.inc.php消失了#TimeMemoryFunctionLocation10.0042244400()... \ login.inc.php:020.0043245592http://www.php.net/PDO.construct'target ='_ new'> __ construct()... \ login.inc.php:10(!)警告:PDO :: _构造():读取问候包时出错 . 第10行的C:\ wamp64 \ www \ DUFT \ login.inc.php中的PID = 9636 Stack#TimeMemoryFunctionLocation10.0042244400()... \ login.inc.php:020.0043245592http://www.php . net / PDO.construct'target =' new'> __ construct()... \ login.inc.php:10OOPs出了什么问题

这是我的logcat所说的:

72768cf4-0633-49e3-8dd1-3583d286d502.png

我的LoginActivity Java类如下所示:

public class LoginActivity extends AppCompatActivity{

//NYT

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds

public static final int CONNECTION_TIMEOUT=2000000000;

public static final int READ_TIMEOUT=2000000000;

private EditText etEmail;

private EditText etPassword;

//NYT

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

//NYT

// Get Reference to variables

etEmail = (EditText) findViewById(R.id.eMail);

etPassword = (EditText) findViewById(R.id.password);

//NYT

TextView klikHer = (TextView) findViewById(R.id.klikHer);

klikHer.setPaintFlags(klikHer.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

Button login = (Button) findViewById(R.id.signIn);

login.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

//Intent intent = new Intent(LoginActivity.this, MenuScreen.class);

//startActivity(intent);

//NYT

// Get text from email and passord field

final String email = etEmail.getText().toString();

final String password = etPassword.getText().toString();

// Initialize AsyncLogin() class with email and password

new AsyncLogin().execute(email, password);

//NYT

}

});

}

private class AsyncLogin extends AsyncTask {

ProgressDialog pdLoading = new ProgressDialog(LoginActivity.this);

HttpURLConnection conn;

URL url = null;

@Override

protected void onPreExecute() {

super.onPreExecute();

//this method will be running on UI thread

pdLoading.setMessage("\tLoading...");

pdLoading.setCancelable(false);

pdLoading.show();

}

@Override

protected String doInBackground(String... params) {

try {

// Enter URL address where your php file resides

url = new URL("http://192.168.87.100/DUFT/login.inc.php");

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return "exception";

}

try {

// Setup HttpURLConnection class to send and receive data from php and mysql

conn = (HttpURLConnection) url.openConnection();

//conn.setReadTimeout(READ_TIMEOUT);

//conn.setConnectTimeout(CONNECTION_TIMEOUT);

conn.setRequestMethod("POST");

// setDoInput and setDoOutput method depict handling of both send and receive

conn.setDoInput(true);

conn.setDoOutput(true);

// Append parameters to URL

Uri.Builder builder = new Uri.Builder()

.appendQueryParameter("username", params[0])

.appendQueryParameter("password", params[1]);

String query = builder.build().getEncodedQuery();

// Open connection for sending data

OutputStream os = conn.getOutputStream();

BufferedWriter writer = new BufferedWriter(

new OutputStreamWriter(os, "UTF-8"));

writer.write(query);

writer.flush();

writer.close();

os.close();

conn.connect();

} catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

return "exception";

}

try {

int response_code = conn.getResponseCode();

// Check if successful connection made

if (response_code == HttpURLConnection.HTTP_OK) {

// Read data sent from server

InputStream input = conn.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(input));

StringBuilder result = new StringBuilder();

String line;

while ((line = reader.readLine()) != null) {

result.append(line);

}

// Pass data to onPostExecute method

return (result.toString());

} else {

return ("unsuccessful");

}

} catch (IOException e) {

e.printStackTrace();

return "exception";

} finally {

conn.disconnect();

}

}

@Override

protected void onPostExecute(String result) {

//this method will be running on UI thread

pdLoading.dismiss();

if (result.equalsIgnoreCase("true")) {

/* Here launching another activity when login successful. If you persist login state

use sharedPreferences of Android. and logout button to clear sharedPreferences.

*/

Intent intent = new Intent(LoginActivity.this, MenuScreen.class);

startActivity(intent);

LoginActivity.this.finish();

} else if (result.equalsIgnoreCase("false")) {

// If username and password does not match display a error message

//Toast.makeText(LoginActivity.this, "Invalid email or password", Toast.LENGTH_LONG).Show();

} else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {

//Toast.makeText(LoginActivity.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).Show();

}

}

}}

我已经按照教程中的所有步骤操作了,通过在我的移动浏览器中输入IPv4地址,我可以通过手机访问本地服务器 . 这必须意味着我也能够访问本地服务器上的数据库,对吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值