[英]call HTTP POST android
Hello this is my code in my android app to verify the login. 您好,这是我在Android应用中用于验证登录名的代码。 This works fine but now i want to call a http POST method in my PHP file so that i can call more than one method.
这工作正常,但现在我想在我的PHP文件中调用http POST方法,以便可以调用多个方法。 How can i do that.
我怎样才能做到这一点。
void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://www.c- iris.com/v2/app/webService.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
// edited by James from coderzheaven.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(AndroidPHPConnectionDemo.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
//startActivity(new Intent(AndroidPHPConnectionDemo.this, UserPage.class));
Intent intent = new Intent(this, UserPage.class);
startActivity(intent);
}else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
This is my PHP file: 这是我的PHP文件:
<?php
include ('../inc_php/databaseConnectie.php');
$username = $_POST['username'];
$password = $_POST['password'];
$accountsQuery = "SELECT * FROM database WHERE email = '".$username."' AND wachtwoord = '".md5($password)."'";
if ($accountsResult = $mysqli->query($accountsQuery))
{
if ($accountsResult->num_rows >= 1)
{
echo "User Found";
}
else
{
echo "No Such User Found";
}
}
?>
For example: 例如:
enter code here
if($_SERVER['HTTP_METHOD'] === 'login')
{
//than execute the code above.......
}
but how can i call this method from android? 但是如何从android调用此方法?
Just comment below line 只需在行下方评论
//Execute HTTP Post Request
response=httpclient.execute(httppost);
In log in method you send data twice with different objects so just remove above line in method. 在登录方法中,您两次发送带有不同对象的数据,因此只需删除方法中的上述行即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.