将字符串从Android发送到PHP

时间:2014-01-07 00:40:48

标签: php android string drupal

这里有许多Android到PHP类型的问题,但没有一个似乎回答了这个问题。我想将我的用户名和密码从我的Android应用程序发送到我的Drupal网站usercrreate.php表单,但我不确定该怎么做。

在我的APP中,我有以下内容:

// Set final values of the UserName and the Password to be stored
String usernameFinal = (String) usernameview.getText();
String passwordFinal = (String) passwordfield.getText();

在我的php表单中我有以下内容:

<?php

define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);

chdir(DRUPAL_ROOT);

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

 //This will generate a random password, you could set your own here
  $password = 'password';

  //set up the user fields
  $fields = array(
    'name' => 'username',
    'mail' => 'sample@email.com',
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    ),
  );

  //the first parameter is left blank so a new user is created
  $account = user_save(NULL, $fields);

  // If you want to send the welcome email, use the following code

  // Manually set the password so it appears in the e-mail.
  $account->password = $fields['pass'];

  // Send the e-mail through the user module.
  drupal_mail('user', 'register_no_approval_required', $email, NULL, array('account' => $account), variable_get('site_mail', 'noreply@example..com'));
  ?>

我的应用会创建两个字符串。测试php表单证明在创建用户帐户方面是成功的,我手动填写包含适当数据的字段。现在我希望APP将字符串发送到php表单并执行url本身。

有想法吗?

1 个答案:

答案 0 :(得分:1)

在你的java中:

HttpClient client=new DefaultHttpClient();
HttpPost getMethod=new HttpPost("http://localhost.index.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username","12345"));
nameValuePairs.add(new BasicNameValuePair("password","54321"));
getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
client.execute(getMethod);

在你的PHP中:

$username = $_POST['username'];
$password = $_POST['password'];