如何将jwt
令牌传递给安全api
并获取json
数据?我用过这段代码。我收到http
未经授权的回复。
String url1 = ("http:/****************/getAllocationsList");
try{
OkHttpClient client = new OkHttpClient();
SharedPreferences prefs = getSharedPreferences(User_Info, MODE_PRIVATE);
final String token = prefs.getString("username", null);
System.out.println("token" + token);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url1);
String json = "";
JSONObject jsonObject = new JSONObject();
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", "Bearer "+token);
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = " Did not work! ";
Log.d("RESULT", result);
System.out.println("request"+result);