Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java okhttp adding headers using for loop dynamically

Many examples showed adding header should:

Request request = new Request.Builder()
    .url("https://api.github.com/repos/square/okhttp/issues")
    .header("User-Agent", "OkHttp Headers.java")
    .addHeader("Accept", "application/json; q=0.5")
    .addHeader("Accept", "application/vnd.github.v3+json")
    .build();

But I want to dynamically add headers as user's header requirements, how can I implement it?

Headers h = new Headers.Builder().build();
for (HttpHeader hh : ht.HttpRequestHeader) {
    h.newBuilder().add(hh.Name, hh.Value);
}  //<<---Nothing changed!!!!!
like image 484
Andy Chan Avatar asked Dec 19 '22 16:12

Andy Chan


1 Answers

Headers.Builder builder = new Headers.Builder();
for (HttpHeader hh : ht.HttpRequestHeader) {
  builder.add(hh.Name, hh.Value);
}
Headers h = builder.build();
like image 105
Jesse Wilson Avatar answered Dec 27 '22 14:12

Jesse Wilson