Volley JsonObjectRequest发布请求忽略参数 [英] Volley JsonObjectRequest Post request ignoring params

查看:164
本文介绍了Volley JsonObjectRequest发布请求忽略参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用凌空连接到API,我正在设置所有参数和标题,但似乎这些参数被忽略了,我在这里缺少什么?上周我开始学习android排球,我有点迷失。

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? 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() {

            }
        }


    }


推荐答案

很明显,有时我们需要在点击网址时提交请求参数。为此,我们必须覆盖 getParams()方法,该方法应返回以键值格式发送的参数列表。

It is obvious that sometimes we need to submit request parameters while hitting the url. To do that we have to override getParams() method which should return list of parameters to be send in a key value format.

所以,在 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;
            }

即。使用下面的代码作为

i.e. 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;
                }
          };

有关详细信息,请参阅 Android与Volley Library合作

For more info see Android working with Volley Library

编辑:

401 是未授权的状态代码。如果您在尝试HTACCESS时遇到 401 ,请参阅问题。您需要使用 Authenticator 传递参数。

401 is the status code for "unauthorized". If you are getting a 401 while trying an HTACCESS see this question. You need to pass the parameters using an Authenticator.

这篇关于Volley JsonObjectRequest发布请求忽略参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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