php - 无法从 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());
}
解决方案
推荐阅读
- pandas - 如何解释熊猫数据框布尔索引的结果
- reactjs - 在 React 中实现 github.io/project 上的相对路径
- c - 除了与函数指针一起使用之外,是否有在 C 中对函数进行类型定义的目的?
- leaflet - 将图像添加到地图并确保纵横比正确
- python - keras:使用 session.graph.as_default(): AttributeError: 'NoneType' 对象没有属性 'graph'
- html - 如何在 Ionic 4 中设置背景图像
- python - /bin/sh: 1: [: 缺失]
- javascript - 使用柯里化技术的返回语句
- mysql - 按触发器排序排名
- javascript - NextJS + Heroku:未加载环境变量