不幸的是,android app停了(数据库连接)

时间:2015-07-15 04:17:11

标签: java android mysql database database-connection

我正在尝试开发一个login registeration页面。在完成了几个教程后,我终于找到了API 22的唯一教程,因为其他教程中使用的http post方法已被弃用。

<<<---Tutorial--->>>

我没有遵循整个教程,因为我一直试图将数据存储到数据库中。我的应用程序运行完美,但当它进入注册过程时,它说我的应用程序“不幸停止”。我是android新手所以请礼貌。以下是我的signup.javabackgroundtask.java个文件。

我在000webhost上托管了一个免费的SQL数据库

public class Signup extends Activity{

    Button signupbutt;
    EditText name , username , userpass , dob , email ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        name = (EditText) findViewById(R.id.editname);
        username = (EditText) findViewById(R.id.edituser);
        userpass = (EditText) findViewById(R.id.editpass);
        dob = (EditText) findViewById(R.id.editdob);
        email = (EditText) findViewById(R.id.editemail);

        signupbutt = (Button) findViewById(R.id.signupbuttonn);
        signupbutt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                userreg(v);
            }
        });
    }

    public void userreg(View view)
    {
        String etname = name.getText().toString();
        String etusername = username.getText().toString();
        String etuserpass = userpass.getText().toString();
        String etemail = email.getText().toString();
        String etdob = dob.getText().toString();
        String method = "Register";
        Backgroundtask backgroundtask = new Backgroundtask(this);
        backgroundtask.execute(method,etname,etusername,etuserpass,etemail,etdob);


    }


}

这是我的 backgroundtask.java

public class Backgroundtask extends AsyncTask<String,Void,Void>{
Context ctx;

Backgroundtask(Context ctx)
{
    this.ctx = ctx;

}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}


@Override
protected Void doInBackground(String... params) {
    // TODO Auto-generated method stub

    String reg_url = "http://8miles.freeiz.com/Register.php";

    String method = params[0];

    String name = params[1];
    String username = params[2];
    String password = params[3];
    String email = params[4];
    String dob = params[5];


    try {
        URL url = new URL(reg_url);

        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        OutputStream os = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
        String data = URLEncoder.encode("name","UTF-8") + "="+URLEncoder.encode("name","UTF-8")+"&"+
                 URLEncoder.encode("username","UTF-8") + "="+URLEncoder.encode("username","UTF-8")+"&"+
                URLEncoder.encode("password","UTF-8") + "="+URLEncoder.encode("password","UTF-8")+"&"+
                URLEncoder.encode("email","UTF-8") + "="+URLEncoder.encode("email","UTF-8")+"&"+
                URLEncoder.encode("dob","UTF-8") + "="+URLEncoder.encode("dob","UTF-8");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onProgressUpdate(Void... values) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
}
}

1 个答案:

答案 0 :(得分:0)

谢谢大家,我没有发送数据 bufferedWriter。写(数据) 而且也没有像这样关闭和刷新缓冲数据 bufferedWriter。红晕 bufferedWriter。关 os.close 此外,我没有在androidmanifest.xml中请求互联网权限