doumeng4400 2015-07-16 18:00
浏览 57

Android,POST数据无法访问php文件

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 MATLAB动图问题
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名