user3052374
user3052374

Reputation:

Send String from Android to PHP

There are many Android to PHP type questions here, but none that seem to answer the question. I would like to send my username and password Strings from me Android app to my Drupal website usercrreate.php form, but am unsure how to do it.

In my APP I have the following:

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

and in my php form I have the following:

<?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' => '[email protected]',
    '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', '[email protected]'));
  ?>

My app creates both strings. Testing the php form proves successful at creating a user account is I fill in the fields with proper data, manually. Now I would like the APP to send the Strings to the php form and execute the url itself.

Got ideas?

Upvotes: 0

Views: 3517

Answers (1)

henry4343
henry4343

Reputation: 3921

In your 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);

In your PHP:

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

Upvotes: 1

Related Questions