简体   繁体   English

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

[英]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. 我正在尝试使用Volley在我的Android应用程序中调用RESTful API。 The API requires that the user get authenticated using a public key and a hash that will be sent via the http header. 该API要求用户使用公共密钥和将通过http标头发送的哈希进行身份验证。

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 当我尝试发送带有公钥和哈希的标头的POST请求时,我发现标头未由服务器附加/接收,并且出现Volley错误-BasicNetwork.performRequest:意外的响应代码400

This is the method that i am trying to use to send the header request with the request parameters using JSON. 这是我尝试使用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 ApplicationConstants类

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 正如您在图片中看到的那样,我在邮递员中提供了公钥和哈希,我还从android的标头中发送了数据,但服务器中未接收到数据

400 means a Bad Request to the server. 400表示对服务器的错误请求。 Maybe the headers are wrong. 标题可能是错误的。 You could try adding the Content-Type=application/json header as follows 您可以尝试添加Content-Type=application/json标头,如下所示

 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. 如果这不起作用,则应在REST客户端中尝试该请求,并查看该请求是否适用于给定的标头。 Make sure the request works fine on the rest client before trying to break your head in Android. 在尝试中断Android之前,请确保该请求在其余客户端上正常运行。

override your getBodyContentType method 覆盖您的getBodyContentType方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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