php - 无法检测到从android发送的php中的Post参数
问题描述
我正在尝试通过 android 应用程序填充 Postgres 数据库。我正在使用 Volley 和 Php 将数据发送到 Postgres 数据库。Post 请求通过浏览器运行良好,即填充数据库,通过 chrome 的 Postman 扩展检查。但是,当我通过 android 执行此操作时,我无法这样做。我也没有收到任何错误。
请在下面的代码中找到我将 x 和 y 发布参数发送到 URL 的位置。
submitbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
builder.setTitle("Server Response");
builder.setMessage("Response: "+response);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(Soil_Info.this, "Clear fields here", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog =builder.create();
alertDialog.show();
Toast.makeText(Soil_Info.this, "Successfull", Toast.LENGTH_SHORT).show();
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Soil_Info.this, "Error Occured", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map <String,String> params=new HashMap<>();
params.put("x", "aisha");
params.put("y", "shah");
return params;
}
};
MySingleton.getInstance(Soil_Info.this).addTorequestque(stringRequest);
stringRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 50000;
}
@Override
public int getCurrentRetryCount() {
return 50000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
下面是PHP代码
<?php
require "connection.php";
if(isset($_REQUEST["x"]) && isset($_REQUEST["y"]))
{
$names = $_REQUEST["x"];
$surnames = $_REQUEST["y"];
// echo $names ;
// echo $surnames ;
$query = "INSERT INTO emp (first, last) VALUES ('$names','$surnames')";
echo $query;
$result = pg_query($conn, $query);
if (!$result) {
die('An error occurred during query.');
} else {
echo ".....Successfull.....";
}
}
else
{
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
pg_close($conn);
?>
字符串 URL = " http://192.168.1.107/test.php ";
解决方案
您正在虚拟主机中运行您的项目,您的浏览器、邮递员等应用程序都可以访问该主机。但是您的手机无法发出有效请求。
你可以做的是:
在真实服务器上部署您的服务器项目,这需要大量工作,但值得。然后你可以通过真实的ip地址访问你的api
用户模拟器代替您的手机
使您的本地主机可访问您的局域网,以便您可以通过手机访问它。看看这个答案的详细信息
推荐阅读
- java - 尽管 Java 已正确安装且 JAVA_HOME 已配置,但 Maven 未编译
- javascript - 如何计算没有匹配的嵌套括号?
- java - ? 将java更改为kotlin时出现null问题
- javascript - React子组件图像不通过传递道具加载
- vue-test-utils - Vue-test-utils wrapper.find 返回 0 处不存在任何项目
- css - iframe 滚动在移动 Safari 中不起作用
- javascript - javascript:如何防止用户在文本框中输入 =?
- web-component - 在插槽元素内强制焦点事件
- bash - bash 或 shell 脚本 - pcap 文件到 csv 文件的转换
- c# - C# 发射,类型值比较