Android Volley:不附加标题

问题描述 投票:0回答:1

我正在尝试在我的Android应用程序中实现标记化,为此我总是需要在我向服务器创建一个授权标头时随时添加授权标头。问题是,当我调试服务器实例时,即使发出请求,也没有添加授权标头

这是我的代码

private void syncDatabases()
{
    String tag_json_obj = "json_obj_req";
    String uri = "random_url";
    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Please wait while syncing");
    pDialog.show();

    List<Plane> allPlane = service.gettAllPlane();
    Gson gson = new Gson();
    JSONArray jsArray = new JSONArray();

    for (Plane p : allPlane)
    {
        String jsonString = gson.toJson(p);
        try {
            JSONObject obj = new JSONObject(jsonString);
            obj.remove("ID");
            jsArray.put(obj);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    JsonArrayRequest jsonObjReq = new JsonArrayRequest(Request.Method.POST, uri, jsArray, new Response.Listener<JSONArray>()
    {
        @Override
        public void onResponse(JSONArray response)
        {
            ArrayList<Plane> newList = new ArrayList<>();
            try
            {
                for(int i=0;i<response.length();i++)
                {
                    String planeName,planeEngine,planeProducer,planeCountry,planeYear,wikiLink,ID;
                    JSONObject pl = response.getJSONObject(i);

                    String p1 = pl.optString("ID");
                    if (pl != null && !p1.isEmpty())
                        ID = pl.getString("ID");
                    else
                        ID = "0";

                    String p2 = pl.optString("planeName");
                    if (p2 != null && !p2.isEmpty())
                        planeName = pl.getString("planeName");
                    else
                        planeName = "";

                    String p3 = pl.optString("planeEngine");
                    if (p3 != null && !p3.isEmpty())
                        planeEngine = pl.getString("planeEngine");
                    else
                        planeEngine = "";

                    String p4 = pl.optString("planeProducer");
                    if (p4 != null && !p4.isEmpty())
                        planeProducer = pl.getString("planeProducer");
                    else
                        planeProducer = "";

                    String p5 = pl.optString("planeCountry");
                    if (p5 != null && !p5.isEmpty())
                        planeCountry = pl.getString("planeCountry");
                    else
                        planeCountry = "";

                    String p6 = pl.optString("planeYear");
                    if (p6 != null && !p6.isEmpty())
                        planeYear = pl.getString("planeYear");
                    else
                        planeYear = "0";

                    String p7 = pl.optString("wikiLink");
                    if (p7 != null && !p7.isEmpty())
                        wikiLink = pl.getString("wikiLink");
                    else
                        wikiLink = "";

                    Plane plf = new Plane(Integer.parseInt(ID),planeName,planeEngine,planeProducer,planeCountry,Integer.parseInt(planeYear),wikiLink);
                    newList.add(plf);
                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }

            service.deleteAllFromPlane();
            for (Plane p : newList)
            {
                boolean ok = service.addNewPlane(p);
            }
            pDialog.dismiss();
            tab1.onIorU();

        }

    }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {
            Log.e("ERROR", "Error occurred ", error);
            pDialog.dismiss();
        }
    })
    {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError
        {
            SharedPreferences settings = PreferenceManager
                    .getDefaultSharedPreferences(MainActivity.this);
            String auth_token_string = settings.getString("token", "");

            Map<String, String> params = new HashMap<>();
            params.put("Content-Type", "application/json; charset=UTF-8");
            params.put("Authorization", auth_token_string);
            return params;
        }
    };

我已经验证了auth_token_string是否为null(它不是)。这是我在服务器端看到的(C#Web API 2服务器)enter image description here

这是我如何获得标题:

var authorization = request.Headers.Authorization;

在图片中你可以看到我尝试在headers字段中发送的令牌的一部分。我究竟做错了什么?谢谢。

java android rest android-volley
1个回答
1
投票

在您的授权令牌中,您缺少授权类型。我假设你正在使用Basic auth,所以试试这个:

params.put("Authorization", "Basic " + auth_token_string);
© www.soinside.com 2019 - 2024. All rights reserved.