【问题标题】:Android Studio: can't write to external databaseAndroid Studio:无法写入外部数据库
【发布时间】:2016-10-03 17:39:46
【问题描述】:

我需要使用 java 和 android studio 从 mysql 数据库在线检索数据。 首先,我尝试将一条记录写入数据库。

这些是我的文件:

init.php

 <?php  
 $db_name = "db";  
 $mysql_user = "admin";  
 $mysql_pass = "password";  
 $server_name = "localhost";  
 $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);  
 ?>

注册.php

 <?php  
 require "init.php";  

 $name = $_POST["user"];  
 $user_name = $_POST["user_name"];  
 $user_pass = $_POST["user_pass"];  
 $sql_query = "insert into user_info values('$name','$user_name','$user_pass');";
 ?>  

我将它们放入服务器文件夹并尝试通过以下方式访问它们:

public class BackgroundTask extends AsyncTask<String, Void, String> {
    AlertDialog alertDialog;
    Context ctx;

    BackgroundTask(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("Login Information....");
    }

    @Override
    protected String doInBackground(String... params) {
        String reg_url = "http://www.trotterellandia.it/register.php";
        String method = params[0];

        if (method.equals("register")) {
            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];
            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                //httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

                String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");

                bufferedWriter.write(data);
                Log.d("Simone", data);

                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return "Registration Success...";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        if (result.equals("Registration Success...")) {
            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }

我使用以下方法调用该方法:

public void userReg(View view) {
    name = ET_NAME.getText().toString();
    user_name = ET_USER_NAME.getText().toString();
    user_pass = ET_USER_PASS.getText().toString();

    String method = "register";

    BackgroundTask backgroundTask=new BackgroundTask(this);`enter code here`
    backgroundTask.execute(method, name, user_name, user_pass);
    finish();
}

但会创建任何行。 代码有什么问题?

【问题讨论】:

  • 您收到什么类型的错误?
  • 我认为没有任何错误。或者,我在哪里看到它?
  • 请问,您确定找到这个条件了吗? if (method.equals("register")) {
  • 是的,我确定。我放了一个日志
  • 警告:使用mysqli 时,您应该使用parameterized queriesbind_param 将用户数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug切勿$_POST$_GET 数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。

标签: android mysql database android-studio phpmyadmin


【解决方案1】:

(这不是答案,只是用来显示格式化代码,谢谢)

在“注册成功”之后...

        return "Registration Success...";
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Toast.makeText(ctx, e.printStackTrace(), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(ctx, e.printStackTrace(), Toast.LENGTH_LONG).show();
        }

这些 Toast 可以显示一些东西。

【讨论】:

  • 不显示任何内容,因为我没有通过 throw catch
  • 您可以继续在 Try 子句中插入 Toast,例如断点,以查看代码运行的位置/验证所有 var 假定的值。
猜你喜欢
  • 2019-02-10
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-12
  • 2020-09-11
  • 2013-05-22
相关资源
最近更新 更多