对不起我是新手,我没有VT技术在我的电脑上安装模拟器所以我想问一下,当你的android应用程序连接到localhost mysql服务器时,可以在手机上测试android应用程序在你的电脑里。
我正在尝试,但我的应用程序没有连接到mysql
如果这可能比我的代码
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
这是我使用异步任务
的backgroundWorker类的代码 protected String doInBackground(String... params) {
String method = params[0];
String weburl = "http://192.168.1.103/And/receive.php"; // this is the localhost pc address
if (method.equals("login")) {
String username = params[1];
String password = params[2];
try {
URL jv = new URL(weburl);
HttpURLConnection httpURLConnection = (HttpURLConnection) jv.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
bf.write(data);
bf.flush();
bf.close();
OS.close();
InputStream is = httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
String result = "";
String line = "";
while ((line = br.readLine()) != null) {
result += line;
}
bf.close();
is.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
这是我的android textfield的代码,它位于主Activity
中public void Login(View v){
String username=usernameEr.getText().toString();
String password=passwordEr.getText().toString();
String type="login";
BackgroundWorker BgWorker= new BackgroundWorker(this);
BgWorker.execute(type,username,password);
}
这是我的PHP代码
<?php
include "connection.php";
$fname=$_POST['user'];
$password=$_POST['password'];
$row=mysql_query("SELECT * FROM tayyab where username='$fname' AND password='$password'") or die("query failed");
$row_count=mysql_num_rows($row);
if($row_count>=1){
echo "You have been logged in!";
}
else{
echo "You have been logged out!";
}
?>
答案 0 :(得分:0)
在您的网址中,您没有包含端口地址。 例如
String url= "http://192.168.0.1:8084/YourApplication