gpt4 book ai didi

android - 在 volley 请求中添加自定义 header

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:11 25 4
gpt4 key购买 nike

我有一个 Volley 请求码

RequestQueue queue = Volley.newRequestQueue(this);
String url =<My URL>;

// Request a string response from the provided URL.
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.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

如何在此设置名为 Authorization 的 header ?

最佳答案

覆盖请求中的 getHeaders,例如:

 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.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("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("Authorization","Your authorization");
//..add other headers
return params;
}
};

关于android - 在 volley 请求中添加自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com