2

I want to create url with dynamic parameters ?

new Uri.Builder()
.scheme("http")
.authority("foo.com")
.path("someservlet")
.appendQueryParameter("param1", foo)
.appendQueryParameter("param2", bar)
.build();

the above Uri class is member of android.net

I have unknown number of params , How can I create dynamic url when I don't know the number of parameters ?

9
  • 1
    you already answered your question: .appendQueryParameter Commented May 14, 2014 at 9:38
  • dude in My answer two parameters are appended , I have multiple parameters for append and I don't know the numbers of parameter Commented May 14, 2014 at 9:45
  • buddy, I know the parameters, but number of parameters is dynamic (not fixed) Commented May 14, 2014 at 9:49
  • so what? iterate over them and call appendQueryParameter... its so easy, whats the problem? Commented May 14, 2014 at 9:50
  • can you please give me solution by code? I have parameters in ArrayListList<NameValuePair> now give me solution bro Commented May 14, 2014 at 9:53

1 Answer 1

2

Given that the nameValuePairs are set before you can first declare the URIBuilder and then add all parameters with their keys in iterative manner through the List.

Uri uri;
Uri.Builder builder = new Uri.Builder();
    builder.scheme("http")
    .encodedAuthority("foo.com")
    .appendEncodedPath("someservlet");

    for (NameValuePair l : nameValuePairs)
    {
        builder.appendQueryParameter(l.getName(), l.getValue());
    }

    uri = builder.build();

Finally you can get the Uri object from the builder object.

For reference how to build a List of NameValuePair please refer to this post: What is the use of List<NameValuePair> or ArrayList<NameValuePair>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.