1
我想連接android應用程序與MySQL數據庫我花了超過14個小時找到錯誤,但我失敗了,我的android應用程序沒有連接到數據庫我已經安裝了wamp服務器在默認端口80上。我正在檢查設備上的Android應用程序。我的設備和開發機連接在同一個路由器上,我已經使用IP路由檢查它Android應用程序沒有連接到mysql數據庫
這是192.168.8.101。我也曾嘗試使用這些網址
String login_url="http://10.0.2.2/receive.php";
String login_url="http://192.168.8.101/receive.php";
String login_url="http://127.0.0.1/receive.php";
所有這些網址無法正常工作
這是我的Android代碼
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context ctx;
AlertDialog alertDialog;
BackgroundWorker(Context ctx){
ctx=ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://10.0.2.2/receive.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onPreExecute() {
alertDialog=new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
這是主要的活動課
public class MainActivity extends AppCompatActivity {
EditText usernameEr,passwordEr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEr=(EditText) findViewById(R.id.editText);
passwordEr=(EditText) findViewById(R.id.editText2);
}
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);
}
}
這是Android清單代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kidsklub.and">
<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"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
這是PHP代碼
<?php
include "connection.php";
$username = $_POST["user_name"];
$password = $_POST["password"];
$row=mysql_query("SELECT * FROM tayyab where username='$username' 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!";
}
?>
你能指出它嗎? –
不工作!我已經在頂部聲明,但仍然沒有響應 –
public class BackgroundWorker extends AsyncTask String result = new String(); 上下文ctx; AlertDialog alertDialog; ' –