如何确保在使用Volley时发送标头请求

时间:2015-07-07 00:15:18

标签: android android-volley

我正在尝试使用Volley在我的Android应用中调用RESTful API。 API要求用户使用公钥和将通过http标头发送的哈希进行身份验证。

当我尝试使用公钥和哈希的标头发送POST请求时,我发现服务器没有附加/接收标头,我得到Volley错误 - BasicNetwork.performRequest:意外的响应代码400

这是我尝试使用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类

public interface ApplicationConstants {

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

As you can see 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

如果我只使用请求参数尝试此操作,但是当我尝试发送标头请求时,它无法发送标头请求。

正如你在pic中看到的那样,我在postman中提供了公钥和哈希,我也从android发送了数据,但是它没有在服务器中收到

2 个答案:

答案 0 :(得分:0)

400表示对服务器的错误请求。也许标题是错误的。 您可以尝试添加Content-Type=application/json标题,如下所示

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

如果这不起作用,那么您应该在REST client中尝试请求,看看它是否适用于给定的标头。在尝试破解Android之前,请确保请求在其余客户端上正常工作。

答案 1 :(得分:0)

覆盖您的getBodyContentType方法

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