dsxxqndv41105 2016-09-29 17:00
浏览 68

从Android应用程序登录MySQL数据库不成功

I have created an android app that logins into a MySQL database but I don't receive any results. The 'result' variable in the alert dialog box in backgroundWorker.java (function "onPostExecute") returns null, can anyone solve this problem?

BackgroundWorker.java` public class BackgroundWorker extends

 AsyncTask<String,String,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker(Context ctx)
    {
      context =ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        String type=params[0];

        String login_url="192.168.10.9/login.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 onPreExecute() {
       alertDialog=new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");

    }

    @Override
    protected void onPostExecute(String Result) {
        Log.i("ok", "Result " + Result);
        alertDialog.setMessage(Result);
        alertDialog.show();

    }

CheckConn.java

public class CheckConn extends AppCompatActivity {
EditText user;
EditText pass;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check_conn);


    user = (EditText) findViewById(R.id.et_u);
    pass = (EditText) findViewById(R.id.et_p);
    Button press = (Button) findViewById(R.id.btn_login);
    press.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onLogin();
        }
    });
}

public void onLogin()
{
    String username= user.getText().toString();
    String password=pass.getText().toString();
    String type="login";

    BackgroundWorker backgroundWorker=new BackgroundWorker(this);

    backgroundWorker.execute(type,username,password);
}

}

connection.php

<?php

 $mysql_usernmae="root";
 $mysql_password="*****";
 $db="map";

 $db= new mysqli('localhost',$mysql_usernmae,$mysql_password,$db);

 ?>

login.php

<?php
 require "connection.php";
 $user_name=$_POST["user_name"];
 $user_pass=$_POST["password"];
 $mysql_qry="select * from user_info where user_name like
 '$user_name' and user_password like 'user_pass';";
  $result=mysqli_query($db,$mysql_qry);
  if (mysqli_num_rows($result) > 0)
 {
  echo "login Successsss";
  }
else
{
echo "faileddddd Booooo";
}
?>

`

  • 写回答

1条回答 默认 最新

  • drfqfuhej48511519 2016-09-29 17:10
    关注

    why you don't ejecute first your login.php file in the browser, harcode the values for user_name and user_pass, and check if the connection is working on server side,

    after that you can check if your user and pass are coming from android app, I must assume you have a database with some data in the table you are making the query, also check if the name of database, table, query are correct, you can check with MySQL workbench if the query works and is returning some result.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?