无法在android中使用httppost

时间:2014-12-30 07:10:04

标签: android http-post

我想发送电子邮件和密码到PHP网址,但我无法做到这一点..我想从edittext获取字符串并将其发送到PHP myadmin localhost我尝试了很多但不能这样做...请帮我完整解释...谢谢:)



<?php
//error_reporting(0);

$con = mysql_connect('localhost','root','');
mysql_select_db('sample_app_ws',$con);

// Insert Query For Send Value To Data Base
//echo $_REQUEST['act'] ;
if(isset($_REQUEST['act']) && $_REQUEST['act'] == 'Insert_UserData') { 
	$email = $_REQUEST['email'];

	$password = $_REQUEST['password'];
	
$result= mysql_query("INSERT INTO  sample_login(email,password,enterDate) VALUES ('".$email."','".$password."',NOW())");
	if($result)	  
	$sendvale ['DataSubmit'] = "DataSubmit";
	else $sendvale ['err'] = "ErrDataSubmit";
	echo json_encode($sendvale);
}
elseif(isset($_REQUEST['act']) && $_REQUEST['act'] == 'Select_UserName'){
	$email = $_REQUEST['email'];
	$password = $_REQUEST['password'];
	
	$result = mysql_query("Select * from sample_login where email = '".$email."' and password = '".$password."'");
	if(mysql_num_rows($result) > 0){
	 $sendvale ['personaldetails'] = "VaildUsrNameAndPassword";
		echo json_encode($sendvale);
	}else{
	 $sendvale ['personaldetails'] = "InVaildUsrNameAndPassword";
		echo json_encode($sendvale);
	}
}
else{
	 $sendvale ['err'] = "POST_DATA_IS_Empty";
		echo json_encode($sendvale);
	}

?>
&#13;
&#13;
&#13;

向我提供与此PHP代码相关的android java代码..

java code ....

public class LoginActivity extends Activity implements OnClickListener {

private EditText userName;
private EditText password;
private Button submit;
private Button cancel;
public static final String URL = " http://127.0.0.1/sample_app_ws/sample_login.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    createObjects();
}

private void createObjects() {

    userName = (EditText) findViewById(R.id.editTextUserName);
    password = (EditText) findViewById(R.id.editTextpassword);
    submit = (Button) findViewById(R.id.buttonLogin);
    submit.setOnClickListener(this);

    cancel = (Button) findViewById(R.id.buttonCAncel);

}

private void sendData(String userName, String Password) {

    ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();

    arrayList.add(new BasicNameValuePair("email", userName));
    arrayList.add(new BasicNameValuePair("password", Password));

    try {

        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);
        httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
        HttpResponse httpResponse = client.execute(httpPost);

        // httpResponse.getEntity().equals("0");

    } catch (Exception e) {

        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }

}

@Override
public void onClick(View v) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            sendData(userName.getText().toString(), password.getText()
                    .toString());
        }
    });

}

}

1 个答案:

答案 0 :(得分:0)

尝试改变这一点:

public static final String URL = " http://127.0.0.1/sample_app_ws/sample_login.php";

到你的IP地址 -

public static final String URL = " http://your IP address/sample_app_ws/sample_login.php";

转到cmd - 编写IPConfig并获取您的IP地址。