Android应用停止。为什么?

时间:2014-02-01 12:02:00

标签: java php android post

所以,我对Android编程很陌生,我正在尝试开发一个简单的活动,它从用户那里获取用户名和密码,并将它们存储在我当前在我的PC上的SQL数据库中。在服务器端,我在表中插入新行的PHP脚本如下:

<?php
mysql_connect("***","***","***") or die("Cannot connect!");
mysql_select_db("webservice") or die("Cannot select DB!");
if(!empty($_POST)){
$us=$_POST['username'];
$pw=$_POST['password'];
$query="INSERT INTO users (username, password) VALUES ('$us', '$pw')";
$res=mysql_query($query);
if($res){
    echo 'Done!';
}
else{
    echo'Error!';
}
}
else{
echo 'no data given!'; 
}
mysql_close();
?>

应用程序中的java代码是:

public class RegisterPost extends Activity {


EditText etUs, etPas;
Button btRe;
public String username, password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register_post);
    etUs = (EditText)findViewById(R.id.etUser);
    etPas = (EditText)findViewById(R.id.etPass);
    btRe = (Button) findViewById(R.id.btReg);
    btRe.setOnClickListener (new OnClickListener(){

        @Override
        public void onClick(View v) {
            //define action when clicking on button
            username=etUs.getText().toString();
            password=etPas.getText().toString();
            tryRegistration(username,password);
        }
    });
}

public void tryRegistration(String user, String pass){

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost/webservice/insert.php");
    try{
      List<NameValuePair> pmts = new ArrayList<NameValuePair>();
      pmts.add(new BasicNameValuePair("username",user));
      pmts.add(new BasicNameValuePair("password",pass));
      post.setEntity(new UrlEncodedFormEntity(pmts));
      HttpResponse response = client.execute(post);
    }
    catch(ClientProtocolException e)
    {
    //auto exc  
    }
    catch(IOException e)
    {
    //auto exc  
    }

}   

}

现在,如果我尝试在模拟器上运行应用程序,我可以输入用户名和密码,但只要按下“注册”按钮,我就会收到消息:     “不幸的是,RegisterUser已停止” 并且应用程序停止。你能帮我么?谢谢!

0 个答案:

没有答案