1
對不起,我是新手,我沒有VT技術在我的電腦上安裝模擬器,所以我想問問是否有可能在手機上測試Android應用程序手機,而你的android應用程序連接到你的電腦上的localhost mysql服務器。是否有可能在連接到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文本框的代碼,這是主要的活動裏面
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!";
}
?>
check plz @RC。我編輯了我的文章 –
@RC。我只是測試不工作的代碼現在沒有sql注入我 –