How to Set Custom Header in Volley Request

How to set custom header in Volley Request

It looks like you override public Map<String, String> getHeaders(), defined in Request, to return your desired HTTP headers.

How to add a header to a request from volley library

I find my problem and the solution is as like as below

I just replace object with JsonObjectRequest and then use overriding getHeaders function

            val request :JsonObjectRequest = object : JsonObjectRequest(Request.Method.POST,
URL,
body,
Response.Listener<JSONObject> { response ->
Log.e(tag, response.toString())
},
Response.ErrorListener{ error ->
Log.e(tag, error.message )
}) {

@Throws(AuthFailureError::class)
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers.put("Content-Type", "application/json");
headers["x-sms-ir-secure-token"] = tokenKey
return headers
}

Add custom headers in volley request

you can write a class extends Request.(override getHeaders() and so on)
just like

public abstract class AbsRequest<T> extends Request<T>{

public AbsRequest(int method, String url, Response.ErrorListener listener) {
this(method, url, null, listener);
}

public AbsRequest(int method, String url, Map<String, String> params, Response.ErrorListener listener) {
this(method, url, params, null, listener);
}

public AbsRequest(int method, String url, Map<String, String> params, Map<String, String> head, Response.ErrorListener listener) {
this(method, url, params, head, null, listener);
}

public AbsRequest(int method, String url, Map<String, String> params, Map<String, String> head, String bodyContentType, Response.ErrorListener listener) {
this(method, url, params, null, head, bodyContentType, listener);
}

public AbsRequest(int method, String url, String body, Map<String, String> head, String bodyContentType, Response.ErrorListener listener) {
this(method, url, null, body, head, bodyContentType, listener);
}

private AbsRequest(int method, String url, Map<String, String> params, String body, Map<String, String> head, String bodyContentType, Response.ErrorListener listener) {
super(method, url, listener);
}
}

for more information you can see https://github.com/Caij/CodeHub/blob/master/lib/src/main/java/com/caij/lib/volley/request/AbsRequest.java
how to use can see https://github.com/Caij/CodeHub/tree/master/app/src/main/java/com/caij/codehub/presenter/imp

How to send Authorization header in Android using Volley library?


StringRequest request = new StringRequest(Request.Method.POST, YourUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (!response.equals(null)) {
Log.e("Your Array Response", response);
} else {
Log.e("Your Array Response", "Data Null");
}
}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error is ", "" + error);
}
}) {

//This is for Headers If You Needed
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("token", ACCESS_TOKEN);
return params;
}

//Pass Your Parameters here
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("User", UserName);
params.put("Pass", PassWord);
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);

Set custom headers to all volley requests

I believe the best way is extend the HttpStack sub-class which you choose to use, then intercepting the performRequest() method, put your global headers inside it.

public class MyHurlStack extends HurlStack {
@Override
public HttpResponse performRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {

Map<String, String> headers = request.getHeaders();
// put your global headers
headers.put("Via", "netroid");
headers.put("Accept-Charset", "UTF-8");
headers.put("Origin", "http://netroid.cn/");

return super.performRequest(request, additionalHeaders);
}
}

public class MyHttpClientStack extends HttpClientStack {
public MyHttpClientStack(HttpClient client) {
super(client);
}

@Override
protected void onPrepareRequest(HttpUriRequest request) throws IOException {
// put your global headers
request.setHeader("Via", "netroid");
request.setHeader("Accept-Charset", "UTF-8");
request.setHeader("Origin", "http://netroid.cn/");
super.onPrepareRequest(request);
}
}

when RequestQueue initializing, use the customize HttpStack instead of.

if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
stack = new MyHttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}

Network network = new BasicNetwork(stack);

Adding Header with Specified Parameter in Volley?


how can the authorization :bearer be added in the header section,which
is shown in the Given picture

Use

headers.put("Authorization", "Bearer " + access_token);

instead of

headers.put("access_token", access_token);


Related Topics



Leave a reply



Submit