In my application I am trying to get the user email address. Using :
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
String possibleEmail = account.name;
...
}
}
I then have a string containing the user's email address. In order to have access to it I try to send this string to a web-server. I have been following this guide : http://www.go4expert.com/articles/step-step-guide-sending-data-android-t30182/
The server I want to use is my website, which is a WordPress hosted by OVH. With OVH I have access to a MySQL Database. The PHP file url is :
private String URL_NEW_PREDICTION = "http://www.domaine.com/new_predict.php";
But nothing is happening, the email is not registred in the database. And in the app I end up in "catch (JSONException e)". I think that the user's smartphone might not have the permission to access the php file. Does it have to do with the .htaccess file? (I don't know much on servers and php, the answer may be trivial.)
Here are the functions of interest in my mainActivity :
public String getemail(){
String possibleEmail="";
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
possibleEmail = account.name;
new AddNewPrediction().execute(langage, possibleEmail);
}
}
return possibleEmail;
}
private class AddNewPrediction extends AsyncTask<String, Void, Void> {
SharedPreferences sharedPref;
@Override
protected void onPreExecute() {
super.onPreExecute();
sharedPref = getApplicationContext().getSharedPreferences("com.setenirdroit.BonnePosture.PREFERENCE_FILE_KEY", 0);
}
@Override
protected Void doInBackground(String... arg) {
String langage = arg[0];
String UserEmail = arg[1];
// Preparing post params
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("langage", langage));
params.add(new BasicNameValuePair("UserEmail", UserEmail));
ServiceHandler serviceClient = new ServiceHandler();
String json = serviceClient.makeServiceCall(URL_NEW_PREDICTION,
ServiceHandler.POST, params);
Log.d("Create Prediction Request: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
boolean error = jsonObj.getBoolean("error");
// checking for error node in json
if (!error) {
// new category created successfully
Log.e("Prediction added successfully ", "> " + jsonObj.getString("message"));
} else {
Log.e("Add Prediction Error: ", "> " + jsonObj.getString("message"));
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "JSON data error!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
ServiceHandler.java has nothing special, and here are the php files We have config.php:
<?php
// Database configuration
define('DB_USERNAME', 'seXXXXit');
define('DB_PASSWORD', 'rJXXXXXXXVB');
define('DB_HOST', 'setXXXXXXXXXoit.mysql.db');
define('DB_NAME', 'seXXXXXit');
?>
And this is new_predict.php
<?php
include_once './DbConnect.php';
function createNewPrediction() {
$response = array();
$langage = $_POST["langage"];
$UserEmail = $_POST["UserEmail"];
$db = new DbConnect();
// mysql query
$query = "INSERT INTO prediction(langage,UserEmail) VALUES('$langage','$UserEmail')";
$result = mysql_query($query) or die(mysql_error());
if ($result) {
$response["error"] = false;
$response["message"] = "Prediction added successfully!";
} else {
$response["error"] = true;
$response["message"] = "Failed to add prediction!";
}
// echo json response
echo json_encode($response);
}
createNewPrediction();
?>
DbConnect.php is not changed.
If you have other idea to get the user's address email I am interested.