我现在正在使用httpurlconnection类我移动到凌空如何才能放置授权令牌
HttpURLConnection connection = (HttpURLConnection) client._url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + client.Token);
提前谢谢
答案 0 :(得分:2)
如何在排球库中接近setRequestProperty
在Volley库HurlStack中使用它我们可以设置setRequestProperty
进行连接:
1。通过扩展HurlStack
类:
public class CustomHurlStack extends HurlStack {
...
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + client.Token);
return connection;
}
}
2。在创建RequestQueue
时,然后传递CustomHurlStack
类对象:
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(),
new CustomHurlStack());
答案 1 :(得分:2)
以下是我在排球项目中经常做的事情:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + mAccessToken);
return headers;
}
对于基本身份验证案例:
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String credentials = "username:password";
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-Type", "application/json");
headers.put("Authorization", auth);
return headers;
}