misaochan
misaochan

Reputation: 890

Uri builder returning jumbled URI

So, I'm trying to append an ID parameter to the end of a URI that the user will be sent to when he clicks on an item in my list. My code is as follows:

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    //items.get(pos) returns the UPI needed. Append to http://www.cs.auckland.ac.nz/our_staff/vcard.php?upi=
    Uri.Builder b = Uri.parse("http://www.cs.auckland.ac.nz/our_staff/vcard.php?upi=").buildUpon();
    b.appendEncodedPath(items.get(pos));
    Uri uri = b.build();
    i.setData(uri);
    Log.d("URL of staff", uri.toString());
    activity.startActivity(i);      
}

Now, I am supposed to get a URI of the form:

http://www.cs.auckland.ac.nz/our_staff/vcard.php?upi=pden001

for example. But the Logcat shows that the URI obtained is actually

http://www.cs.auckland.ac.nz/our_staff/vcard.php/pden001?upi=

Why does it append pden001 to the middle?

I have tried appendPath() as well with the same results, and the Android Developer Tutorial is not very helpful in this case.

Upvotes: 1

Views: 1173

Answers (1)

Nate
Nate

Reputation: 31045

The Uri builder is handling the base URI differently from the query parameters, but you've combined them in this string:

"http://www.cs.auckland.ac.nz/our_staff/vcard.php?upi="

What I think you should do is leave ?upi= off of your string literal, and then append your upi parameter and pden001 value using the appendQueryParameter() method:

  //items.get(pos) returns the UPI needed. Append to http://www.cs.auckland.ac.nz/our_staff/vcard.php
  Uri.Builder b = Uri.parse("http://www.cs.auckland.ac.nz/our_staff/vcard.php").buildUpon();
  b.appendQueryParameter("upi", items.get(pos));
  Uri uri = b.build();
  i.setData(uri);

Upvotes: 1

Related Questions