简体   繁体   English

Volley Android库的Http基本授权

[英]Http basic authorization with Volley Android Library

I used below code to open a file but dose not work and message "BasicNetwork.performRequest: Unexpected response code 401 for http://..... " is showed. 我使用下面的代码打开文件,但无法正常工作,并显示消息“ BasicNetwork.performRequest:http:// .....的意外响应代码401”。 what is wrong with android and volley? android和volley有什么问题?

   StringRequest sr = new StringRequest(Method.POST,url,
           new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                pages = PageParser.parse(response);
                adapter = new DailyReportAdapter(MenuDailyReport.this,
                        pages);
                lv.setAdapter(adapter);
                pDialog.hide();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // error.printStackTrace();
                pDialog.hide();
                Toast.makeText(MenuDailyReport.this,
                        "Could not connect to server"+error.getMessage(), Toast.LENGTH_LONG)
                        .show();
                finish();
            }
        })
{

        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

            params.put("username","mohammad" );
            params.put("password","mohammad123" );
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            return params;
        }
    ;
};

The problem could be the charset, must be utf-8, i thinks. 我认为问题可能是字符集,必须是utf-8。 So 所以

Try to Override this methos, inside off override getheaders(), as it: 尝试覆盖此方法,在覆盖getheaders()的内部,如下所示:

    @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }

I recommend to use Bearer Token as header, is better. 我建议使用Bearer Token作为头,更好。 Regards. 问候。

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

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