简体   繁体   English

如何使用 Volley 将 POST 请求发送到本地服务器 PHP 脚本?

[英]How to send POST request to local server PHP script using Volley?

I am trying to POST a value to a script running on a local server.我正在尝试将值发布到在本地服务器上运行的脚本。 My code is given below :我的代码如下:

 final String data = "poet=nazmul";
        //Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
        StringRequest request = new StringRequest(Request.Method.POST, "http://192.168.1.112:8080/api/post.php?" + data, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), volleyError.toString(), Toast.LENGTH_LONG).show();
            }
        });
        AppController.getInstance().addToRequestQueue(request);

But the local server does not respond.但是本地服务器没有响应。 My php code is:我的php代码是:

<?php
 $poet = $_POST['poet']; 
 echo json_encode(array('poet'=>$poet));
?>

When I open the URL directly in a web browser, I get this error:当我直接在 Web 浏览器中打开 URL 时,出现此错误:

Notice: Undefined index: poet in C:\\xampp\\htdocs\\api\\post.php on line 2 {"poet":null}注意:未定义索引:在 C:\\xampp\\htdocs\\api\\post.php 中的第 2 行中的诗人 {"poet":null}

How can I solve this problem?我怎么解决这个问题? The main problem was windows firewall, it was preventing me from connecting to the local server, after disabling it and changing the php code $_POST to $_GET everything seems working fine.Thanks everyone for their help.主要问题是 windows 防火墙,它阻止我连接到本地服务器,在禁用它并将 php 代码 $_POST 更改为 $_GET 后,一切似乎都正常。谢谢大家的帮助。

You're doing a get request because of the concatenation of ?由于 ? 的串联,您正在执行 get 请求。 and + data.和 + 数据。 If you change the server script $_POST to $_GET and change the Request.Method.POST to Request.Method.GET this will work.如果您将服务器脚本 $_POST 更改为 $_GET 并将 Request.Method.POST 更改为 Request.Method.GET 这将起作用。

You're submitting data to your script using POST (which doesn't matter in this case, but if it was changing something -- not idempotent -- you must do).您正在使用 POST 向脚本提交数据(在这种情况下这无关紧要,但如果它正在改变某些东西——不是幂等的——你必须这样做)。 Typically, data submitted using POST will have a request body that is sent along with the request, and not using URL parameters as you have here.通常,使用 POST 提交的数据将有一个与请求一起发送的请求正文,而不是像这里那样使用 URL 参数。

Both are fine, but unfortunately PHP's $_POST dictionary only gets values from the message body, and not the URL parameter (which $_GET references).两者都很好,但不幸的是 PHP 的$_POST字典只从消息正文中获取值,而不是 URL 参数( $_GET引用的)。 To access them, you will need to parse the request URI yourself , by accessing the $_SERVER['REQUEST_URI'] variable.要访问它们,您需要通过访问$_SERVER['REQUEST_URI']变量自己解析请求 URI

If you are never going to make changes to your server's state with this request, it is appropriate to change the request type to GET.如果您永远不会使用此请求更改服务器的状态,则将请求类型更改为 GET 是合适的。 Please read this question and answers for more insight into the differences between GET and POST and using URL parameters.请阅读此问题和答案,以更深入地了解 GET 和 POST 之间的差异以及使用 URL 参数。

Alternatively, if you don't want to change the PHP script at all, you need to modify your Android code to send the data in the request body (and not as a URL parameter).或者,如果您根本不想更改 PHP 脚本,则需要修改您的 Android 代码以在请求正文中发送数据(而不是作为 URL 参数)。 Please see this question on some approaches to doing that.请参阅有关执行操作的一些方法的此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM