调用带有标头的Volley请求时出现UnsupportedOperationException [英] UnsupportedOperationException while calling Volley request with headers
本文介绍了调用带有标头的Volley请求时出现UnsupportedOperationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试通过Volley库发出呼叫请求.我想设置标题,但是我正在获取java.lang.UnsupportedOperationException.您知道为什么吗,如何解决这个问题?
I try to make call request with Volley library. I want to set headers, but I'm getting java.lang.UnsupportedOperationException. Do you know why , and how can I solve this problem ?
public void getAccountInfo() {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = "http://demo.gopos.pl/oauth/token";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Log.e(TAG, "onResponse SUCCES!!" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "That didn't work!");
}
}) {
@Override
public Map<String,String> getHeaders()throws AuthFailureError {
Map<String,String> params = super.getHeaders();
if(params==null)params = new HashMap<>();
params.put("username","username");
params.put("password","password");
return params;
}
};
queue.add(stringRequest);
}
推荐答案
删除这两行:
Map<String,String> params = super.getHeaders();
if(params==null)params = new HashMap<>();
并添加以下内容:
Map<String, String> params = new HashMap<>();
现在应该可以正常工作了.
It should work now.
这篇关于调用带有标头的Volley请求时出现UnsupportedOperationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文