Java Android截击Json请求:参数未显示

Java Android截击Json请求:参数未显示,java,android,json,android-volley,Java,Android,Json,Android Volley,我正在做一个简单的应用程序,此时你所要做的就是按下连接按钮,请求批准锁定请求。如果被授予,则会出现JSON响应。这需要一个HTTP头X-API-key,我将其粘贴到strings.xml中。我在标题中指定了它。我也包括了参数。我已经在《邮递员》中测试了URL,它运行良好。此时会出现一条错误消息。正如你们将在下面看到的,我有两个类:Main Activity和MySingleton。先谢谢你 public class MainActivity extends AppCompatActivity {

我正在做一个简单的应用程序,此时你所要做的就是按下连接按钮,请求批准锁定请求。如果被授予,则会出现JSON响应。这需要一个HTTP头X-API-key,我将其粘贴到strings.xml中。我在标题中指定了它。我也包括了参数。我已经在《邮递员》中测试了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中替换此代码


您能粘贴错误消息吗?我正在手机上运行,响应为:{err:,statusCode:..无效的输入参数…缺少'lockName'参数。我认为这是服务器端的lockName参数有问题。请尝试在运行代码之前从postman REST客户端发布数据。对lockName使用与邮递员请求中相同的密钥。我使用的是相同的密钥。在旧代码中,connect.setOnClickListenernew View.OnClickListener{@Override public void onClickView v{你是否用上述代码替换了connect.setOnClickListener中的现有代码?是的,我已经替换了,现在我在按下手机上的按钮时收到403错误代码。如果可能,你能提供x-api-key吗?我是这个论坛的新手,所以我不知道这一点。是的,让我们将此移到聊天室。我现在还不允许使用聊天室。api键:sOdgqxUJmm6lKAObnI2Dq5JYv6f4NVot9KMiNMWL
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);