凌空不发送一个带参数的post请求。 [英] Volley not sending a post request with parameters.

查看:135
本文介绍了凌空不发送一个带参数的post请求。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code以下(排球图书馆通过谷歌)发送POST请求我的PHP服务器,并获得一个结果信息。我试过code,而不检查使用isset($ _ POST ['身份证'])在PHP和code正常工作。买的时候,我开始检查,PHP将if语句跳过并转到别的意思是code未正确发送 PARAMS。我该如何解决这个问题?

I have the code below (Volley Library By Google) to send a POST request to my php server and get information as a result. I tried the code without checking isset($_POST['id']) in php and the code worked fine. Buy when I started to check, php will skip the if statement and go to else meaning the code is not sending the params correctly. How can I fix this?

RequestQueue queue = Volley.newRequestQueue(Chat.this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
        CHAT_URL_FEED, null,
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d("THISSSSSSSS", response.toString());
                if (response != null) {
                    parseChatJsonFeed(response);
                }
            }
        }, new Response.ErrorListener(){

    @Override
    public void onErrorResponse(VolleyError error){
        VolleyLog.d("Here", "Error: " + error.getMessage());
    }
}) {

    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id);
        return params;
    }

};

queue.add(jsonObjReq);

我也试过以下code:

I also tried the following code:

RequestQueue queue = Volley.newRequestQueue(Chat.this);

        JSONObject params = new JSONObject();
        try {
            params.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                CHAT_URL_FEED, params,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("THISSSSSSSS", response.toString());
                        if (response != null) {
                            parseChatJsonFeed(response);
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("Here", "Error: " + error.getMessage());
            }
        });

        queue.add(jsonObjReq);

但我仍然得到同样的结果。

but I still get the same result.

推荐答案

花更多的时间寻找到这个问题后,我发现凌空不能正常用POST请求JSON请求工作。用户@SMR建议,在我的问题的注释部分。我也看到在谷歌群体和混帐勉回购了类似的答案。我结束了使用GET请求来传递信息到服务器,检索JSON饲料。

After spending some more time looking into this problem, I found out that Volley does not properly work with JSON request with POST requests. User @SMR suggested that in the comment section of my question. I also saw a similar answers on Google groups and the mian repo on git. I ended up using GET requests to pass the information to the server and retrieve a JSON feed.

这篇关于凌空不发送一个带参数的post请求。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆