Android Php登录错误,namevalued对发送空白数据

时间:2012-10-22 02:17:42

标签: php android

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

Android代码 -

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";
}


?>  

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

1 个答案:

答案 0 :(得分:0)

UrlEncodedFormEntity takes an arument of type List.

尝试更改

ArrayList<NameValuePair> nameValuePairs;

List<NameValuePair> nameValuePairs;

将此行保持不变

nameValuePairs = new ArrayList<NameValuePair>(2);