简体   繁体   English

Volley JsonObjectRequest发布请求忽略参数

[英]Volley JsonObjectRequest Post request ignoring params

I'm trying to connect to an API using volley, I'm setting all the parameters and headers, but it seems that the params are being ignored, what am I missing here? 我正在尝试使用凌空连接到API,我正在设置所有参数和标题,但似乎这些参数被忽略了,我在这里缺少什么? I started learning android volley last week and I'm kind of lost. 上周我开始学习机器人凌空,我有点迷失了。

    package com.rep.app.principal;

    import android.os.AsyncTask;
    import android.os.Bundle;

    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;

    import com.actionbarsherlock.app.SherlockFragmentActivity;
    import com.android.volley.AuthFailureError;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.VolleyLog;
    import com.android.volley.toolbox.JsonObjectRequest;
    import com.android.volley.toolbox.Volley;
    import com.rep.R;


    import org.json.JSONObject;

    import java.util.HashMap;

    import java.util.Map;


    public class InicioActivity extends SherlockFragmentActivity {


       RequestQueue queue = null;




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            txtDisplay = (TextView) findViewById(R.id.txtDisplay);

            queue=Volley.newRequestQueue(this);


            AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask();
            mAutenticacaoLocalTask.execute((Void) null);

        }
        private TextView txtDisplay;



        public class AutenticacaoLocalTask extends AsyncTask<Void, Void, Boolean> {

            @Override
            protected Boolean doInBackground(Void... params) {


                try {


                    txtDisplay = (TextView) findViewById(R.id.txtDisplay);

                    String url = "http://192.168.1.18/opa/api/";


                   JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                System.out.println(response);

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

                            }
                        })

                   {

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {

                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkDaaDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                        return headers;
                    }

   @Override 
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", "rm@test.com.br");
                    params.put("senha", "aaa");

                    return params;
                }
          };    

              queue.add(jsObjRequest);
                    return true;

                } catch (Exception e) {
                    Log.e("RM", e.getMessage());
                    return false;
                }

            }

            @Override
            protected void onPostExecute(final Boolean success) {

            }

            @Override
            protected void onCancelled() {

            }
        }


    }

It is obvious that sometimes we need to submit request parameters while hitting the url. 很明显,有时我们需要在点击URL时提交请求参数。 To do that we have to override getParams() method which should return list of parameters to be send in a key value format. 为此,我们必须覆盖getParams()方法,该方法应返回以键值格式发送的参数列表。

So,Override getParams() in JsonObjectRequest as 所以,在JsonObjectRequest覆盖getParams()

            @Override 
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("email", "rm@test.com.br");
                params.put("senha", "aaa");

                return params;
            }

ie use below code as 即使用下面的代码作为

 JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                System.out.println(response);

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

                            }
                        })

                   {

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {

                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkDaaDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                        return headers;
                    }

   @Override 
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("email", "rm@test.com.br");
                    params.put("senha", "aaa");

                    return params;
                }
          };

For more info see Android working with Volley Library 有关更多信息,请参阅Android与Volley Library合作

EDIT: 编辑:

401 is the status code for "unauthorized". 401是“未授权”的状态代码。 If you are getting a 401 while trying an HTACCESS see this question . 如果您在尝试HTACCESS时获得401 ,请参阅此问题 You need to pass the parameters using an Authenticator . 您需要使用Authenticator传递参数。

Instead of putting params along a request, try with the third parameter of the JsonObjectRequest object, which is a JSON object ( jsonobj in my code). 而不是在请求中放置参数,尝试使用JsonObjectRequest对象的第三个参数,它是一个JSON对象(我的代码中为jsonobj )。

Something like: 就像是:

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {
        System.out.println("onResponse()");

        ...
    }
});

Check my answer here . 在这里查看我的答案。

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

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