4

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?

Nick Betcher
  • 2,026
  • 5
  • 19
  • 25
  • I copy-pasted your code, and it gives me 08-11 07:36:40.554: VERBOSE/HTTPGet(364): `testurl.toString == http://www.google.com` – Daniel Fekete Aug 11 '11 at 07:37
  • @Nick, might this log line be coming from somewhere else? Are you logging the phrase `testurl.toString` anywhere else? Somehow, an array is being printed out, as indicated by `[L` ... – Ray Toal Aug 11 '11 at 07:45
  • @Ray Toal You're right and I apologize, the context was not proper. I should have not assumed. The uri.toString() context is in a private class that extends an AsyncTask and the uri is created outside of that class in its parent (calling) class. Is it because of these class boundaries the issue arises? Or is it because I'm using an external resource (JSoup)? Here is the full code: http://pastebin.com/Cei7exin – Nick Betcher Aug 11 '11 at 08:30
  • Oh of course, this all makes perfect sense now! I will update my answer! – Ray Toal Aug 11 '11 at 08:42

2 Answers2

5

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

Shash316
  • 2,218
  • 18
  • 19
5

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

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Actually, the documentation says that `toString` "returns the encoded string representation of this URI. Example: "http://google.com/". The behaviour OP is experiencing is therefore unexpected, to say the least. – Xion Aug 11 '11 at 07:37
  • In that case convert the android Uri to a Java URI and call `toString` on that: information at http://stackoverflow.com/questions/559902/android-how-can-i-convert-android-net-uri-object-to-java-net-uri-object – Ray Toal Aug 11 '11 at 07:40
  • @Xion Agreed that this is odd, `[L` indicates an array. – Ray Toal Aug 11 '11 at 07:43
  • Thanks for the newly clarified answer! I'm new to Java from C++ and this helped clarify a lot. – Nick Betcher Aug 11 '11 at 09:19