-2

I need to send encoded username and Password through request body. Here I'm sending username and password in the format of JSON object.

How to do it?

Thanks in advance.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Surekha
  • 333
  • 1
  • 5
  • 17
  • 1
    `Thanks in advance.`. No. We want thank after you have been helped. – greenapps Apr 20 '17 at 07:03
  • At last I found a solution. Actually Base64 class is not available in Android, so you need to add this Base64 class in your project. [Here is a link](http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html) for further help. – Abdul Mateen Feb 27 '18 at 16:19

2 Answers2

2

Try

Encode

String encodedUsername = URLEncoder.encode(username);
String encodedPassword = URLEncoder.encode(password);

Decode

String decodedUsername = URLDecoder.decode(encodedUsername);
String decodedPassword = URLDecoder.decode(encodedPassword);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
1

A simple Google search would've done the trick...

URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.

String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");

...

Source: Java URL encoding of query string parameters

Community
  • 1
  • 1
Denny
  • 1,766
  • 3
  • 17
  • 37