简体   繁体   中英

How to ensure that header request are sent when using Volley

I am trying to call a RESTful API in my Android app using Volley. The API requires that the user get authenticated using a public key and a hash that will be sent via the http header.

When I try to do send a POST request with the headers of the public key and the hash, I find that the header is not appended / received by the server and I get Volley error - BasicNetwork.performRequest: Unexpected response code 400

This is the method that i am trying to use to send the header request with the request parameters using JSON.

 HashMap<String, String> params = new HashMap<String, String>();


                params.put("email", email.getText().toString());
                params.put("password", password.getText().toString());

                pDialog = new ProgressDialog(LoginActivity.this);
                pDialog.setMessage("Logging in...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();

                RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
                JsonObjectRequest request = new JsonObjectRequest(ApplicationConstants.url_sign_in, new JSONObject
                        (params), new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            Log.d(TAG, "onResponse") ;
                            error = response.getBoolean(TAG_ERROR);
                            message = response.getString(TAG_MESSAGE);
                            Toast.makeText(LoginActivity.this, message + " value of error", Toast.LENGTH_LONG).show();
                            pDialog.cancel();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                        Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();


                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d(TAG,"onErrorResponse()") ;
                        pDialog.cancel();
                        NetworkResponse networkResponse = error.networkResponse;
                        if (networkResponse != null && networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED) {
                            // HTTP Status Code: 401 Unauthorized
                            Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
                        }


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

                        HashMap<String, String> headers = new HashMap<String, String>();
                        headers.put(ApplicationConstants.publicKey, "Yahoo");
                        headers.put(ApplicationConstants.hash, "Google");
                        Log.d(TAG,"getHeaders()") ;
                        return headers;
                    }

                };

                requestQueue.add(request);

ApplicationConstants class

public interface ApplicationConstants {

//String ip = "10.0.3.2";
String ip = "192.168.100.2";
String publicKey = "pskPublicKey";
String hash = "pskPublicKey";

如您所见,我在邮递员中提供了公钥和哈希,我还从android的标头中发送了数据,但服务器中未接收到数据

If I try this with only the request parameters it works but when when i try to send a header request it fails to send the header request.

As you can see in the pic I supplied the public key and the hash in postman and I also sent data in the header from android but it is not being received in the server

400 means a Bad Request to the server. Maybe the headers are wrong. You could try adding the Content-Type=application/json header as follows

 header.put("Content-Type", "application/json");

If this doesn't work then you should try the request in the REST client and see if it works with the given headers. Make sure the request works fine on the rest client before trying to break your head in Android.

override your getBodyContentType method

public String getBodyContentType()
    {
        return "application/json";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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