Failed to send parameter to PHP POST parameter android

末鹿安然 提交于 2019-12-02 11:59:24
 void MakePostRequest() {
            StringRequest postRequest = new StringRequest(Request.Method.POST, EndPoints.BASE_URL_ADS,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonResponse = new JSONObject(response);
                                value1= jsonResponse.getString("Your ID1");
                                value2= jsonResponse.getString("Your ID2");

                            } catch (JSONException e) {
                                e.printStackTrace();
                                banner_id = null;
                                full_id = null;
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                            value1= null;
                            value2= null;
                        }
                    }
            ) {
           // here is params will add to your url using post method
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("app", getString(R.string.app_name));
                    //params.put("2ndParamName","valueoF2ndParam");
                    return params;
                }
            };
            Volley.newRequestQueue(this).add(postRequest);
        }

This post request is using this compile com.mcxiaoke.volley:library:1.0.19 volley version.

i am just adding app name as parameter.you can add more params.

Best of luck

You'd better check the question separately for Server and Android, as you mentioned sometimes Server works good.

Server check

  1. You can use Postman,to test whether Server is ok, or on what conditions, Server is bad. Especially check your Server's response is standard Json format. If you want do it easier, temporarily change $_POST to $_GET, and install Chrome JSONView. Then test it by your Chrome.
  2. Check ProductID format in your databases. If it is text, you'd better first $product_id = $_POST['ProductID'], and then use "$product_id"

Android check

  1. I strongly suggest use StringRequest instead of JsonObjectRequest, manually convert String to JSONObject. If response string is not JSONObject, then you can print it and find exception.
  2. use below code to create JSONObject params: Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("ProductID", 3); JSONObject params = new JSONObject(paramsMap);

Hope to help you.

to debug more, you can add at the top of the php code the following var_dump($_POST); die();

and then see in the android what are the values printed, if you get nothing it means the android didn't send the request correctly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!