-The first way : Using Uri.Builder class
Let's say that i want to create the following URL :
https://www.myawesomesite.com/turtles/types?type=1&sort=relevance#section-nameUri.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");
URL myUrl = new URL(builder.build().toString);
-The second way : when we just need to append/add variables to the available URL
http://api.example.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7
final String FORECAST_BASE_URL = "http://api.example.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
.appendQueryParameter(QUERY_PARAM, params[0]) .appendQueryParameter(FORMAT_PARAM, "json") .appendQueryParameter(UNITS_PARAM, "metric") .appendQueryParameter(DAYS_PARAM, Integer.toString(7)) .build();
At last :
Source : http://stackoverflow.com/questions/19167954/use-uri-builder-in-android-or-create-url-with-variables
Không có nhận xét nào:
Đăng nhận xét