-2

For example I have this string:

String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?username=USERNAME&password=PASSWORD";

and I have the String USERNAME="123456" and String PASSWORD="123"; When I make the URL GET request how do I inject the USERNAME and PASSWORD into the URL?

2 Answers2

0

You can just use the Replace function.

This will give you the following code:

String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?username=USERNAME&password=PASSWORD";
String USERNAME="123456";
String PASSWORD="123";

url = url.replace("?username=USERNAME&password=PASSWORD","username="+USERNAME+"&password="+PASSWORD);

Although it doesn't seem wise to send a Username and Password in the querystring! If no other option is available be sure to use https so this is not visible for sniffing tools. Otherwise use the default authentication.

Jeroen VL
  • 341
  • 5
  • 9
  • Why would you do that? Android has URI builder class specifically for this – OneCricketeer Feb 03 '17 at 08:14
  • If he starts with this full string you will have to cut it first at the '?' so you can use the builder to add the querystring parameters. A String function is always needed. Then it seems shorter to just replace it? – Jeroen VL Feb 03 '17 at 08:19
  • You'd leave off the question mark for the builder. It adds it. Surely you'd recall whatever method again for different usernames and passwords, so not sure about shorter – OneCricketeer Feb 03 '17 at 08:22
0

You can create a method like below

public String getUrl(String Username,String Password){
    String url = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?username="+Username+"&password="+Password;
    return url;
}

After that you can call this method

String URL = getUrl("Username","Password");
Narendra Motwani
  • 1,085
  • 10
  • 20