【问题标题】:Php script not processing data that it is receiving from androidPHP脚本不处理它从android接收的数据
【发布时间】:2017-05-30 06:55:25
【问题描述】:

我正在尝试将参数传递给我的 php 脚本。数据被传递,但总是返回“成功”为 0。我通过在传递值后回显查询来测试 php,它工作正常并放入所有数据。即使var_dump($_POST)表示它接收到数据,但它总是返回“成功”为0。

添加参数:

params.add(new BasicNameValuePair("states", states));
params.add(new BasicNameValuePair("min_budget", min_budget));
params.add(new BasicNameValuePair("max_budget", max_budget));
params.add(new BasicNameValuePair("activity", activity));
Log.d("Passing parameters: ", params.toString());

// Check your log cat for JSON response
try {
    JSONObject json = jParser.makeHttpRequest(url_all_resorts, "POST", params);

makeHttpRequest方法:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
Log.d("Passing parameters 2: ", params.toString());

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
Log.d("Entity 2: ", httpEntity.toString());
is = httpEntity.getContent();`

logcat:

Log.d("Passing parameters 2: ", params.toString());
Passing parameters 2:: [states=Himachal Pradesh, min_budget=1,000, max_budget=9,000, activity=Leopard Safari]

由 android 收到:

Data received:: array(4) {
    ["states"]=>
    string(16) "Himachal Pradesh"
    ["min_budget"]=>
    string(5) "1,000"
    ["max_budget"]=>
    string(5) "9,000"
    ["activity"]=>
    string(14) "Leopard Safari"
}

{"success":0,"message":"No resort found"}

【问题讨论】:

    标签: java php android json


    【解决方案1】:

    在你的 php 中尝试这个开头,对于我的应用它确实有效:

    if (isset($_SERVER['HTTP_ORIGIN'])) {
            header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Max-Age: 86400');    // cache for 1 day
            }
    
        // Access-Control headers are received during OPTIONS requests
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    
            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
                header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
    
            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                header("Access-Control-Allow-Headers:  {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
            exit(0);
        }
    
        $postdata = file_get_contents("php://input");
        if (isset($postdata)) {
    
            $request = json_decode($postdata);
            $username = "";
            $password = "";
        //here use your values
        if (!empty($request->password)) {
                 $password = $request->password;
                }
                if (!empty($request->username)) {
                $username = $request->username;
                }
    (...)
    

    只需确保您在 httpPost 中正确指向 php 文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 2015-06-19
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      相关资源
      最近更新 更多