1

我想在 android 中创建一个连接到我的 sql 数据库的登录页面

安卓代码——

public class Hetro01Activity extends Activity implements OnClickListener {

    EditText uname;
    EditText password;
    Button ok;
    ArrayList<NameValuePair> nameValuePairs;
    @Override
    public void onCreate(Bundle savedInstanceState) {         
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ok=(Button)findViewById(R.id.submit);

        ok.setOnClickListener(this);


    }

    public void onClick(View vu)

    {
        switch(vu.getId())
        {
        case R.id.submit:

             uname=(EditText)findViewById(R.id.uname);
             password=(EditText)findViewById(R.id.password);

            nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("uname",uname.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("password",password.getText().toString().trim()));


        String result=null;
        InputStream is=null; 
        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.hetroservers.orgfree.com/login1/checklogin.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response=httpclient.execute(httppost);

            HttpEntity entity=response.getEntity();

            is=entity.getContent();
            Log.e("response",is.toString());    
            try
            {
                BufferedReader br=new BufferedReader(new InputStreamReader(is),8);
                StringBuilder sb=new StringBuilder();
                String line=null;

                while((line=br.readLine())!=null)
                {
                    sb.append(line+"\n");

                }
                is.close();

                result=sb.toString();
                result= result.replaceAll("\\s+",""); 
                Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();  
                //validate(result);
                      }
            catch(Exception e)
            {
                  Log.e("log_tag2", "Error");
            }



        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        }


        }   
        }

    public void validate(String res)
    {
        if(res.equals("Session Created"))
        {

            Intent myInt=new Intent(this,Main1.class);
            startActivity(myInt);

        }
        else
        {


        }

    }
    }

而php代码是

mysql_connect("$host", "$user", "$pass")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$myusername=$_REQUEST['uname']; 
$mypassword=$_REQUEST['password']; 

$insert="INSERT INTO hetrologin(username,password) VALUES ('$myusername','$mypassword')";
$init=mysql_query($insert);

$sql="SELECT username FROM hetrologin WHERE username= '$myusername' AND password= '$mypassword'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count == 1){
echo "1";
}
else{
echo "Wrong Username or Password";
}


?>  

我想检查错误出现在哪里,所以使用插入语句来检查我的数据库,我注意到无论我给数据库的输入是什么,都只存储用户名和密码的空白值,当我通过 get 请求尝试相同时从我的浏览器服务器数据正在正确存储。知道为什么吗?

4

1 回答 1

0
UrlEncodedFormEntity takes an arument of type List.

尝试改变

ArrayList<NameValuePair> nameValuePairs;

List<NameValuePair> nameValuePairs;

让这一行保持不变

nameValuePairs = new ArrayList<NameValuePair>(2);
于 2012-10-22T03:22:16.650 回答