4

I looked at the source code for URLConnection.setRequestProperty() in android studio, and it doesn't seem like it sets anything:

public void setRequestProperty(String field, String newValue) {
        checkNotConnected();
        if (field == null) {
            throw new NullPointerException("field == null");
        }
    }

and here is checkNotConnected():

private void checkNotConnected() {
        if (connected) {
            throw new IllegalStateException("Already connected");
        }
    }

What am I missing here? HttpURLConnection "the extending class" has no implementation of setRequestProperty(), so it seems like this method does absolutely nothing.

Farshad
  • 3,074
  • 2
  • 30
  • 44
  • It prevents of setting property after connection and checks the field to ensure it's not null, right? – Farshad Apr 06 '16 at 21:11
  • Yes, but if the connection hasn't been made, and the field is not null, it should set the request property, right? Where is the code that sets the request property? –  Apr 06 '16 at 21:13
  • You're right, even if you look carefully the connect method is abstract too with nobody, the implementation is in a compiled jar file – Farshad Apr 06 '16 at 21:38
  • Just click on the "overriden icon with arrow" next to the method declaration and choose your desired implementation too review – Farshad Apr 06 '16 at 21:40
  • I don't get it. I see the "overriden icon with arrow," but what do those classes mean as far using android's implementation of HttpURLConnection? I have a HttpURLConnection object - httpURLConnection. What does it matter what other subclasses of URLConnection override setRequestProperty(). If I call httpURLConection.setRequestProperty(), shouldn't it check that class for an implementation, and then if not not that class than its superclasses? Why would it use an implementation from a "sibling" class? –  Apr 06 '16 at 22:29
  • I got what you are asking, check this: http://stackoverflow.com/questions/34168030/which-implementation-of-httpurlconnection-is-used-for-android – Farshad Apr 06 '16 at 23:09
  • And this: http://stackoverflow.com/questions/34154572/where-is-connect-and-disconnect-implemented – Farshad Apr 06 '16 at 23:09
  • Thanks for the links @NaN, but I think they still leave me wanting for a more conclusive answer. When I ctrl click a class or method in android studio, doesn't that bring me to the class or method that's being implemented at runtime? If not, what am I looking at when I ctrl click a class or a method? –  Apr 07 '16 at 00:50

1 Answers1

0

URLConnection is an abstract base class that does not implement any specific protocols.

You're looking for HttpURLConnection. This implementation of the URLConnection class overrides and functionally implements the setRequestProperty() method for use with the HTTP protocol.

UPDATE

After closer inspection I found that HttpURLConnection is also an abstract class. It is possible in Android Studio to find any implementations by right clicking the class declaration and clicking Go To -> Implementation(s).

I doubt however that the actual concrete implementation is shipped with the SDK that we work with. I only found Java specific implementations (rt.jar).

This might mean that the actual implementation of setRequestProperty() is not included in the SDK.

whitebrow
  • 2,015
  • 21
  • 24
  • http://developer.android.com/reference/java/net/JarURLConnection.html also extends URLConnection but probably deals with request properties differently, hence the abstract class. – whitebrow Apr 06 '16 at 23:12
  • Actually if you check the implemention of HttpURLConnection.setRequestProperty, the arguments never been used in the body of method and no super is called, If I mistake not khakis is asking for where the property set. – Farshad Apr 06 '16 at 23:19
  • Exactly, @NaN. To my understanding of java, if a method is not implemented in a child class, the method that is used at runtime is the implementation that is in the closest relative superclass; and being that the hierarchy goes HttpURLConnection --> URLConnection --> Object, it seems it would be the implementation of setRequestProperty() in URLConnection that is called. –  Apr 07 '16 at 00:38
  • Why would the concrete implementation not be shipped with the SDK that we work with? As developers, shouldn't we know how the tools we're using work? –  Apr 07 '16 at 00:43