dongningwen1146 2019-03-07 07:13
浏览 88

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

I'm using android volley StringRequest to send a post request to a PHP file which is already hosted online. This was working before but after the domain name was changed, this PHP file no longer receives the volley request as a post request. In the PHP file $_SERVER['REQUEST_METHOD'] returns GET instead of POST.

Here is the android volley request code snippet

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

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? Or maybe the PHP version of the new domain? Please help out

  • 写回答

1条回答 默认 最新

  • doudao2407 2019-03-07 14:28
    关注

    I removed the "www" from the url and it solved my problem. This SO question POST Requests seen as GET by server helped

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog