Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
Nick Betcher Avatar asked Dec 27 '22 15:12

Nick Betcher


2 Answers

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

like image 87
Ray Toal Avatar answered Dec 30 '22 10:12

Ray Toal


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

like image 22
Shash316 Avatar answered Dec 30 '22 11:12

Shash316