1

I am using Django REST framework TokenAuthentication. I am able to recieve token from api when i sends username and password. But how to send that token with every request from android app to the server?

2 Answers 2

2

You can save the Token in your shared preferences so you can access it everywhere in your app: so you dont have to give it with the intent everytime

final SharedPreferences prefs = this.getSharedPreferences( "PACKAGE NAME", Context.MODE_PRIVATE);

you can put the Token in it by

prefs.edit().putString("Token",tokenvalue).apply();

and retrieve it by

prefs.getString("Token","DEFAULT VALUE");

You can send the Token with your request by putting it in the Headers:

HttpPost httpPost = new HttpPost(); httpPost.setHeader("authorization",prefs.getString("Token","DEFAULT VALUE");

Sign up to request clarification or add additional context in comments.

5 Comments

Your answer is useful. Everything is fine but problem is HttpPost is deprecated
URLConnection replaced all the apache classes HttpClient etc.
Can you provide me the code in context of URLConnection?
coderanch.com/t/362398/Servlets/java/… I think that will do the trick
also can you tell me how to add header in AsyncHttpClient(loopj) ?
0

You need to include the token in the header you send with the request, where the random string is your token:

Authorization: Token 1af538baa9045a84c0e889f672baf83ff24

3 Comments

i am able to use that if i make a request from a client. I tested this using requestmaker.com. But in android can you tell me how to set header?
apache library and its functions has been deprecated from api 22
do we have alternate for that? AsyncHttpClient by loopj is available but i don't how to add header in that...Can you help me out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.