简体   繁体   English

我无法将android volley的POST请求发送到php

[英]I can't send POST request from android volley to php

I'm using android volley StringRequest to send a post request to a PHP file which is already hosted online. 我正在使用android volley StringRequest将发布请求发送到已经在线托管的PHP文件。 This was working before but after the domain name was changed, this PHP file no longer receives the volley request as a post request. 此操作在域名更改之前但已生效,但此PHP文件不再将截击请求作为发布请求接收。 In the PHP file $_SERVER['REQUEST_METHOD'] returns GET instead of POST. 在PHP文件$_SERVER['REQUEST_METHOD']返回GET而不是POST。

Here is the android volley request code snippet 这是android volley请求代码片段

StringRequest request = new StringRequest(Request.Method.POST, postURL, new Response.Listener<String>(){
        @Override
        public void onResponse(String s) {
            Log.d("MY_DEBUG",s);

        }
    },new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    }) {
        @Override
        public Map<String, String> getHeaders()
        {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("Connection", "Keep-Alive");
            return parameters;
        }

        @Override
        public String getBodyContentType() {
            //return "application/x-www-form-urlencoded; charset=UTF-8";
            return "application/x-www-form-urlencoded";
        }

        //adding parameters to send
        @Override
        protected Map<String, String> getParams()  {
            Map<String, String> parameters = new HashMap<>();
            parameters.put("request", "edit_product");
            parameters.put("id", id);

            return parameters;
        }
    };

    request.setShouldCache(false);
    InitiateVolley.getInstance().addToRequestQueue(request);

And here are the few first lines of code in the PHP file that handles the post request from an android volley 这是处理来自Android Volley的发布请求的PHP文件中的前几行代码

if($_SERVER['REQUEST_METHOD']=='POST'){
//it's a POST request, We are good to go...handle the post request.
}else{
  //not a POST request, kill it here
  echo "error";
  die();
}

What could be causing this issue? 可能导致此问题的原因是什么? Is there a setting I have to do on the PHP side? 我需要在PHP方面进行设置吗? Or maybe the PHP version of the new domain? 还是新域的PHP版本? Please help out 请帮忙

I removed the "www" from the url and it solved my problem. 我从网址中删除了“ www”,它解决了我的问题。 This SO question POST Requests seen as GET by server helped 这样的问题POST请求被服务器视为GET帮助

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

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