Naga
Naga

Reputation: 2021

Best Practice to build URL in android

What is the best practice to build Http Uri in Android. I want to use Uri.Builder to build the url.

I want to create a class which will return all the Http urls including parameters.

I checked the below link for Uri.Builder which gives idea to utilise in case of get request but how to use the power of Uri.Builder in post request too.

Use URI builder in Android or create URL with variables

Over all I am asking for best practice to build Http Urls(Get and post both ) by utilising power of Uri.Builder

Upvotes: 1

Views: 5570

Answers (1)

ThomasThiebaud
ThomasThiebaud

Reputation: 11969

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.

Upvotes: 2

Related Questions