How to encode username and password? - android

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.

Try
Encode
String encodedUsername = URLEncoder.encode(username);
String encodedPassword = URLEncoder.encode(password);
Decode
String decodedUsername = URLDecoder.decode(encodedUsername);
String decodedPassword = URLDecoder.decode(encodedPassword);

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

Related

How to get this data in json fromat?

I got this data in PayUMoney and to show the user But problem how to get this data in json or key value pair format any one know that please help!!
addedon=2019-11-21+17%3A06%3A42&productinfo=FINE&firstname=Creataum+Test+User&
Assuming that u have got it as String following code will print the key value pair from the string
String x ="addedon=2019-11-21+17%3A06%3A42&productinfo=FINE&firstname=Creataum+Test+User&";
String pair[] = x.split("&");
Log.e("pairs", Arrays.asList(pair).toString());
for (int i=0;i<pair.length;i++){
String key[] = pair[i].split("=");
Log.e("pair:","key= "+ key[0]+" value= "+key[1]);
}
It seems the string is both URL encoded (the %3A) and JSON encoded (the &).
You have to decode the string and then split by '&' and then split each pair to key and value by '='.
You can see here how to JSON decode: Decoding JSON String in Java
URL decoding can be done with Java's URLDecoder class: https://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html
For this example I'll just assume the only encoded characters are %3A and &.
String payumoney = "addedon=2019-11-21+17%3A06%3A42&productinfo=FINE&firstname=Creataum+Test+User&";
// String decoding.
payumoney = payumoney.replaceAll("%3A", "-");
payumoney = payumoney.replaceAll("&", "&");
HashMap<String, String> params = new HashMap<String, String>();
String[] pairs = payumoney.split("&");
for (String pair : pairs) {
String[] keyValue = pair.split("=");
params.put(keyValue[0], keyValue[1]);
}
This looks like a String with mixture of URL encoding (like %3A for :) and HTML characters (like & for &).
I believe you can try decoding it and then split data like say by & to get data for each key and then further split by = to get value for that key.
For URL decoding you can try like
String decoded = java.net.URLDecoder.decode(url, "UTF-8");
You can simply use String replaceAll() function to create the json.
str= str.replaceAll("=", "\"");
str= str.replaceAll("&", "\",\"");
str= "{\""+ str+ "\"}";

How to change format String from Json response?

I want to ask. I got a string json response like this
"Bank Danamon|Reksa Dana Insight Money Syariah"
And i want to change that string to like this in Android setText
Bank Danamon
Reksa Dana Insight Money Syariah
This is the code when i set the response
txvSettlementName.setText(itemSettlement.getAccountName());
Is there a way to do that?
Thanks
You have to split the string like
String str = "Bank Danamon|Reksa Dana Insight Money Syariah";
String[] separated = str.split("|");
separated[0]; // this will contain "Bank Danamon"
separated[1]; // this will contain " Reksa Dana Insight Money Syariah"
String accountName = itemSettlement.getAccountName();
accountName = accountName.replace("|","\n");
txvSettlementName.setText(accountName);
This code will directly replace the character | with line break \n which you can directly set to TextView

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

How to remove brackets and quotation marks from JSON String

I have a ArrayAdapter the places JSON strings into a list view. The problem is when it places the strings into the textview it leaves brackets and quotation marks. For example, if the string contained the name Bob. The string would show up in the ListView as ["Bob"]. How do I remove the brackets and quotation marks?
Here is what I use to get the JSON strings,
String username = json2.getString(KEY_USERNAME);
String number = json2.getString(KEY_NUMBER);
String content = json2.getString(KEY_COMMENT);
tempList2.add (new Item (username, number, content));
customAdapter.addAll(tempList2);
You can use a simple replace:
String bob = "[\"Bob\"]";
bob = bob.replace("[", "").replace("\"", "").replace("]", "");
Log.i("Test", bob);
This will now just print Bob
As an addon to Phil's answer, the end quote (in my chrome browser) did not get removed. I fixed it by using .replace("\"", "") again at the end.

Not able to get name/value pairs from JSON object, when using variable

Not able to get name/value pairs from JSON object, when using the variable but able to read it when hard coding the name.
To better explain :
1) My JSON object is like this -
{.....
{ "rates":{ "name1": value1, "name2": value2 ...etc }
...}
2) I am able to read this object in my android app.
3) Now this rate object name value pairs, i am trying to read based on user input -
String s1 = '"'+name1+'"'; // here name1 i got from user input, & converted into string
4) Now when i am trying to get the value from rates object, i am getting null exception -
JSONObject rateObject = jObject.getJSONObject("rates"); //able to get
complete object
String rate1 = (String) rateObject.get(s1); // giving NULL exception
5) But if i use hard code string, it works -
String rate1 = (String) rateObject.get("name1"); // working
Any pointers why its not working while using variable.
thanks
Thanks for suggestions, i sorted out the problem. There are 2 mistakes i was doing - 1) Using the quotes as correctly pointed out by others and 2) casting the double value to string. Correcting both has resolved my problem :)
In terms of your final code snippet, you are actually doing
String rate1 = (String) rateObject.get("\"name1\""); //note the extra quotes
because you have bookended the user input string with double-quote characters. You just want the input string itself with no bookending. The quotes in the JSON notation serve to delineate each key name; the quotes are not part of the key name itself.
You need to omit the quotes when you create s1:
String s1 = name1;
Or, if name1 is not a String already:
String s1 = name1.toString();
Replace:
String s1 = '"'+name1+'"';
with:
String s1 = name1;

Categories

Resources