在android中维护会话

时间:2016-06-07 09:20:17

标签: java android

如何将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);

0 个答案:

没有答案