Android Studio:无法写入外部数据库

时间:2016-06-03 15:18:20

标签: android mysql database android-studio phpmyadmin

我需要使用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);  
 ?>

register.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();
}

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

1 个答案:

答案 0 :(得分:0)

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

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

        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可以展示一些东西。