Nick Betcher
Nick Betcher

Reputation: 2046

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

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?

Upvotes: 4

Views: 11317

Answers (2)

Ray Toal
Ray Toal

Reputation: 88468

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. :)

Upvotes: 5

Shash316
Shash316

Reputation: 2218

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

Upvotes: 5

Related Questions