0

I was looking for an easy way to do a GET request given a URL, port and path. I came across this method which works fine but I started wondering why it is designed to be like this verbose. Why isn't it better for the URL class to provide a get() method that just does steps 2,3 and 4 behind the scenes?

    URL url = new URL("http", host, port, path);
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);
    conn.connect();
3
  • Not sure what you are asking about. Where do you see redundant data and/or steps? Commented Sep 5, 2014 at 3:51
  • I was thinking along the lines of having a url.get() method that includes: URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.connect(); Commented Sep 5, 2014 at 4:00
  • 3
    You are actually asking why the API is not more high level than it already is. This is just the way it was designed; having a higher level API would prevent developers from accessing many other options that are available now. You can still write your own wrapper ! Commented Sep 5, 2014 at 4:06

2 Answers 2

1

Indeed, the Apache HttpClient is a better replacement of URLConnection. The HttpClient has been integrated into Android API and it is more friendly to developer. More can be seen at http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

Sign up to request clarification or add additional context in comments.

Comments

0

url.openStream() bypasses the need for a self-managed URLConnection object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.