【问题标题】:not able to work with httppost in android无法在 android 中使用 httppost
【发布时间】:2014-12-30 07:10:04
【问题描述】:

我想向 php url 发送电子邮件和密码,但我无法做到这一点.. 我想从 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);
	}

?>

为我提供与此 php 代码相关的 android java 代码..

java代码....

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());
        }
    });

}

}

【问题讨论】:

  • 我正确不知道我使用了一些来自网络的代码,这些代码正在使用 Listvalue 对和 httpclient 和 post 方法,但没有任何事情发生,即使日志正在打印任何东西......你能给我一些代码 sn支持我的php代码的android java的-p
  • 你的 Java 代码是什么?
  • 查看我发布的java代码...
  • @segarci 你在那里??你得到我的代码了吗?你想对我的代码有什么建议吗??

标签: android http-post


【解决方案1】:

尝试改变这一点:

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 地址。

【讨论】:

  • 我们是否总是需要添加。 jSonparser 类和在你的代码中你只添加值......为此我必须转换 hp 代码但我想要相同的 php 代码不想改变它......
猜你喜欢
  • 2016-08-15
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多