在我的应用程序中,我试图获取用户的电子邮件地址。 使用:
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;
...
}
}
然后我有一个包含用户电子邮件地址的字符串。为了能够访问它,我尝试将此字符串发送到Web服务器。 我一直在关注本指南: http://www.go4expert.com/articles/step-step-guide-sending-data-android-t30182/
我想使用的服务器是我的网站,这是由OVH托管的WordPress。使用OVH,我可以访问MySQL数据库。 PHP文件网址是:
private String URL_NEW_PREDICTION = "http://www.domaine.com/new_predict.php";
但没有发生任何事情,电子邮件未在数据库中注册。在应用程序中,我最终进入" catch(JSONException e)"。 我认为用户的智能手机可能没有访问php文件的权限。是否与.htaccess文件有关? (我对服务器和php不太了解,答案可能很简单。)
以下是我的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没什么特别的,这里是php文件 我们有config.php:
<?php
// Database configuration
define('DB_USERNAME', 'seXXXXit');
define('DB_PASSWORD', 'rJXXXXXXXVB');
define('DB_HOST', 'setXXXXXXXXXoit.mysql.db');
define('DB_NAME', 'seXXXXXit');
?>
这是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未更改。
如果您有其他想法获取我感兴趣的用户地址电子邮件。