Android App未连接mysql数据库 [英] Android App is not connected to mysql database

查看:39
本文介绍了Android App未连接mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect android app with mysql database i have spent more than 14 hours to find the error but i am failed and my android app is not connecting to the database i have installed wamp server on port 80 which is by default . I am checking my android app on the device . My device and the development machine is connected on the same router and i have used ip route to check it

即 192.168.8.101 .我也试过这些网址

which is 192.168.8.101 . I have also tried with these urls

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

所有这些网址都不起作用

all these urls are not working

这是我的安卓代码

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清单代码

this is the android manifest code

<?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.

Return result varible from doInBackground method call. you are returning null.

class 顶部声明 result 变量.

Declare result variable in top of class.

这篇关于Android App未连接mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆