java - Android Volley Json 请求 : Parameters not showing

标签 java android json android-volley

我正在做一个简单的应用程序,此时您所要做的就是按连接按钮来请求授予锁定请求。如果获得批准,则会显示 JSON 响应。这需要一个 HTTP header (X-API-key),我将其粘贴到 strings.xml 中。我在标题中指定了它。我也包括了参数。我已经在 postman 中测试了该 URL,它工作正常。这里会出现一条错误消息。正如您将在下面看到的,我有两个类:Main Activity 和 MySingleton。先感谢您。

public class MainActivity extends AppCompatActivity {

    private Button connect;
    private ImageView mImageView;
    private TextView textResponse;
    private Map<String, String> headers;
    private Map<String, String> mParams;

    String lockUrl = "https://lzx650r9ih.execute-api.us-west-2.amazonaws.com/p01/lock";


     @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.imageView);
        connect = (Button) findViewById(R.id.connect_button);
        textResponse = (TextView) findViewById(R.id.textResponse);

        //press the connect button to request a lock
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, lockUrl, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {
                                textResponse.setText("Response: " + response.toString());
                                connect.setText(R.string.Connection_lock);
                                //if the lock is granted then the green button appears
                                mImageView.setImageResource(R.drawable.bullet_green);
                            }

                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        connect.setText(R.string.Connection_no);
                        //if no connection then the red button appears
                        mImageView.setImageResource(R.drawable.circle_red);
                        VolleyLog.e("Error: ", error.getMessage());

                    }

                }) {

                    @Override
                    public Map<String, String> getParams()throws AuthFailureError {
                        HashMap<String, String> mParams = new HashMap<String,String>();
                        mParams.put("nameSpace", "accounting");
                        mParams.put("lockName", "kevin1");
                        mParams.put("durationSeconds", "600");
                        mParams.put("userParam", "Cust-20221");
                        mParams.put("callbackUrl","www.yahoo.com");
                        return mParams;
                    }


                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put("x-api-key", getResources().getString(R.string.parse_api_key));
                        return headers;
                    }

                };

                MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
            }
        });
        }}

单例:

public class MySingleton {

    private static MySingleton mInstance;
    private static Context mCtx;
    private RequestQueue requestQueue;

    private MySingleton(Context context) {
        mCtx = context;
        requestQueue = getRequestQueue();
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return requestQueue;
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        return mInstance;
    }

    public <T> void addToRequestQueue(Request<T> request) {
        requestQueue.add(request);
    }
}

最佳答案

将此代码替换为setOnClickListener

String lockUrl = "https://lzx650r9ih.execute-api.us-west-2.amazonaws.com/p01/lock?nameSpace=accounting&lockName=kevin1qq&durationSeconds=600&userParam=Cust-20221&callbackUrl=www.yahoo.com";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, lockUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("RES  :  ", "RES  IS  " + response);
                    textResponse.setText("Response: " + response.toString());
                    connect.setText(R.string.Connection_lock);
                    //if the lock is granted then the green button appears
                    mImageView.setImageResource(R.drawable.bullet_green);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    connect.setText(R.string.Connection_no);
                    //if no connection then the red button appears
                    mImageView.setImageResource(R.drawable.circle_red);
                    VolleyLog.e("Error: ", error.getMessage());
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("x-api-key", "sOdgqxUJmm6lKAObnI2Dq5JYv6f4NVot9KMiNMWL");
            return headers;
        }
    };

    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

关于java - Android Volley Json 请求 : Parameters not showing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44127388/

相关文章:

android - 如何在android中使用SQLite数据库从数据库中选择数据?

javascript - Google Closure - 将数据形成为 json 对象

java - JSON 中 "keep"换行符的好方法?

c# - C#中使用JSON反序列化成字典

java - 尝试从URL Java播放声音

java - 在我的应用程序的 ListView 上查看 GoogleDrive 文件

java - 使用流如何创建摘要对象

java - 围绕 Spring-AOP 切入点和继承的澄清

android - 如何为 ListView 中的单个元素设置动画

android - 从youtube播放列表中获取视频ID并下载它们