gpt4 book ai didi

php - Android App 连接不上mysql数据库

转载 作者:行者123 更新时间:2023-11-30 22:06:53 25 4
gpt4 key购买 nike

我正在尝试将 android 应用程序与 mysql 数据库连接 我花了超过 14 个小时才找到错误,但我失败了,我的 android 应用程序没有连接到数据库 我已经在默认端口 80 上安装了 wamp 服务器.我正在设备上检查我的 android 应用程序。我的设备和开发机器连接在同一个路由器上,我已经使用 ip route 来检查它

这是 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";

所有这些 url 都不起作用

这是我的安卓代码

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);
}
}

这是主要的 Activity 类

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 list 代码

<?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!";

}

?>

最佳答案

doInBackground 方法调用返回 result 变量。您正在返回 null

class 顶部声明 result 变量。

关于php - Android App 连接不上mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com