Use Uri Builder in Android or Create Url with Variables

Use URI builder in Android or create URL with variables

Let's say that I want to create the following URL:

https://www.myawesomesite.com/turtles/types?type=1&sort=relevance#section-name

To build this with the Uri.Builder I would do the following.

Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("www.myawesomesite.com")
.appendPath("turtles")
.appendPath("types")
.appendQueryParameter("type", "1")
.appendQueryParameter("sort", "relevance")
.fragment("section-name");
String myUrl = builder.build().toString();

URI builder in Android, odd URL

In order to modify query, you may use appendQueryParameter for plain strings like "|", any you may use encodedQuery for pre-encoded strings like "%7C".

There is no append method for pre-encoded query parameters.

Best Practice to build URL in android

There are not differences between a GET and a POST url. The http request method will be different but URI can be the same.

For example check this one and this one. Both of them use the same resource Url but differents methods.

Uri.Builder is made to create Uri, you can use it for both GET and POST. So to build the Uri https://api.twitter.com/1.1/account/settings.json (from the example above) I can use this code

Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("api.twitter.com")
.appendPath("1.1")
.appendPath("account")
.appendPath("settings.json");
Uri uri = builder.build();

I think you are searching for something like a HttpRequest.Builder but I don't know one. You can still use a third party library like retrofit.

Building URL using URI builder

I think you are misunderstanding what your query params are. They should each be key-value pairs in your url. For your url https://spreadsheets.google.com/tq?tqx=out:JSON&tq=select * where ((A= 201))&key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU, building it would look something like:

final String STUDENT_RECORD_URL="https://spreadsheets.google.com/tq";

Builder builder = Uri.parse(STUDENT_RECORD_URL).buildUpon();
builder.appendQueryParameter("txq", "out:JSON");
builder.appendQueryParameter("tq", "select * where ((A= 201))");
builder.appendQueryParameter("key", "1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU");

String finalUri = builder.build().toString();

The appendQueryParameter method will automatically url encode your parameters.

Android: append Query Parameter without a key in URI Builder

There is no function to addQueryParam without value, so you may add some dummy value, "", for example, and then replace it with empty string (replace("=", "")).

But I recommend you to build query by himself and then append queryString with "query" function of Uri.Builder.

public static URL buildUrl(String token, String item) {
String query = new StringBuilder()
.append(PARAM_TOKEN).append("=").append(token).append("&")
.append(PARAM_ITEM).toString();

Uri builtUri = Uri.parse(BASE_URL).buildUpon()
.encodedQuery(query)
.build();

URL url = null;
try {
url = new URL(builtUri.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}

return url;
}

But it requires BASE_URL to has no any query params. If it has then you have to build new Uri from parts of the old one, but replace query with your own query builded from BASE_URL query and your additional params

Android Uri.Builder using / instead of // after scheme

You need to set the URL path to the authority(), instead to the path which will always give you 1 forward slash. remove .path("www.gravatar.com") add authority("www.gravatar.com"),

Here is more info why authority is used.

Java Use URI builder?

If you want to create an UrlBuilder using the builder pattern, it could be done like this:

public class UrlBuilder {
private final String root;
private int myParam1;
private String myParam2;

public UrlBuilder(final String root) {
this.root = root;
}

public UrlBuilder myParam1(int myParam1) {
this.myParam1 = myParam1;
return this;
}

public UrlBuilder myParam2(String myParam2) {
this.myParam2 = myParam2;
return this;
}

public URL build() throws MalformedURLException {
return new URL(
String.format("%s/%d/%s", root, myParam1, myParam2)
);
}
}

Then you will be able to create your URL as next

URL url = new UrlBuilder("http://www.example.com/api/Video/GetListMusicRelated")
.myParam1(25)
.myParam2("jim")
.build();

NB: This only shows the idea, so I used fake parameter's name and incorrect number of parameters, please note that you are supposed to have 6 parameters and set the proper names.

uriBuilder.scheme not adding :// in the URL

Try

uriBuilder.scheme("https")
.authority("test")
.appendQueryParameter("code", "S256")

Uri-Difference between appendPath() and appendQueryParameter()

appendPath() is for path segments and appendQueryParameter() for query params with key value (in your example Date=12012017).

Check this link for more info and examples:
Use URI builder in Android or create URL with variables



Related Topics



Leave a reply



Submit