首页 > 解决方案 > 无法从 PHP 中的 $_POST 变量获取数据到 Android Studio

问题描述

我想从 PHP 托管文件中获取 $_POST 变量的内容到 Android 应用程序中。我曾尝试使用 Jsoup 和 Volley 库来执行此操作,但它们没有产生预期的结果。后来,发现它echo ("anyString");正在被提取到android,但echo($_POST["orderId"]);即使它被打印在网页中也没有被提取。有没有办法解决这个问题?

这是PHP代码:

<?php

     $secretkey = "7457645673urgjnjkf784jyj66545y";
     $orderId = $_POST["orderId"];
     $orderAmount = $_POST["orderAmount"];
     $referenceId = $_POST["referenceId"];
     $txStatus = $_POST["txStatus"];
     $paymentMode = $_POST["paymentMode"];
     $txMsg = $_POST["txMsg"];
     $txTime = $_POST["txTime"];
     $signature = $_POST["signature"];
     $data = $orderId.$orderAmount.$referenceId.$txStatus.$paymentMode.$txMsg.$txTime;
     $hash_hmac = hash_hmac('sha256', $data, $secretkey, true) ;
     $computedSignature = base64_encode($hash_hmac);

     if ($signature == $computedSignature) {
         echo json_encode($_POST);
     }

 ?>

Android中的java代码:

URL link = new URL("https://sample.php");
HttpURLConnection conn;

conn = (HttpURLConnection) link.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setDoOutput(true);

int response_code = conn.getResponseCode();

// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {

    // Read data sent from server
    InputStream input = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    StringBuilder result = new StringBuilder();
    String line;

    while ((line = reader.readLine()) != null) {
        result.append(line);
    }
    Log.d("result", result.toString());
}
}catch (Exception e){
Log.d("result", e.toString());
}

标签: phpandroid

解决方案


推荐阅读