Uri.toString() returns a reference instead of the String

10,844

Solution 1

ORIGINAL ANSWER (Scratch it)

Uri.toString writes out a description of the URI object from the class Uri.

Documentation is here: http://developer.android.com/reference/android/net/Uri.html

To get the human readable version, invoke the getter methods defined for the class.

THE REAL ANSWER

The OP has clarified and provided the actual code. Here is the actual context:

@Override
protected Document doInBackground(Uri... arg0) {
    Document ret = null;
    Log.v("HTTPGet", "Uri.toString == " + arg0.toString());
    try {
        ret = Jsoup.connect(arg0.toString()).get();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

What is happening here is that the parameter arg0 has type Uri[], namely an array of Uri. The dot-dot-dot syntax is Java's "varargs". It means the parameter is actually an array, but rather than passing an array in the call, you can pass any number of arguments that Java will bundle up into an array.

Because you are using a third party library, you have to override this method which takes in one or more Uris. You are assuming that only one will be used. If this is the case, you should instead write

Log.v("HTTPGet", "Uri.toString == " + arg0[0].toString());

If you really will be processing multiple uris, use a for-loop to go through and log all of them.

Make sure to fix your Jsoup.connect line too. It doesn't want a messy array string. :)

Solution 2

Invoke getEncodedPath() on Uri to get the string in it.

Something like below

// imageUri is an Uri extracted from Intent 
String filePath = imageUri.getEncodedPath();

This filePath will have string content as defined in Uri. i.e. content:/media.../id

Shash

Share:
10,844
Nick Betcher
Author by

Nick Betcher

Certified Pharmacy Technician up until 2015; now a Senior Software Developer at the most well-known healthcare insurance provider in the United States. At my position I have -- and still do -- practice nearly all of my life knowledge of technology: from Linux administration to software development; from IT security to [scientifically-practiced] troubleshooting; Kubernetes to Pega; and far beyond. The experience and expertise I can provide will benefit all Stack Exchange users; and if it doesn't, then chances are I'll learn something new. And no matter who you are, we all still have something new to learn, so I try to embrace being wrong -- you should too! At least then it'll mean you're that much less likely to be wrong next time! 🙃

Updated on June 04, 2022

Comments

  • Nick Betcher
    Nick Betcher almost 2 years

    When attempting to execute this code in an Android Activity:

    Uri testurl = Uri.parse("http://www.google.com");
    Log.v("HTTPGet", "testurl.toString == " + testurl.toString());
    

    the only output in Logcat is a reference to a string, but not the string itself:

    HTTPGet(23045): testurl.toString == [Landroid.net.Uri;@4056e398
    

    How can I print the actual string?

  • Xion
    Xion over 12 years
    Actually, the documentation says that toString "returns the encoded string representation of this URI. Example: "google.com". The behaviour OP is experiencing is therefore unexpected, to say the least.
  • Ray Toal
    Ray Toal over 12 years
    In that case convert the android Uri to a Java URI and call toString on that: information at stackoverflow.com/questions/559902/…
  • Ray Toal
    Ray Toal over 12 years
    @Xion Agreed that this is odd, [L indicates an array.
  • Nick Betcher
    Nick Betcher over 12 years
    Thanks for the newly clarified answer! I'm new to Java from C++ and this helped clarify a lot.