How to Get a new HttpURLConnection

public URLConnection openConnection () was added in API level 1
Returns a new connection to the resource referred to by this URL.
Throws: IOException - if an error occurs while opening the connection.

1. Create a new URL object named myUrl.

2. Declare a new HttpURLConnection named myHttpURLConnection.
3. In the file, add the below line to the imports section. 


4. Add the below catch statement under the MalformedURLException catch you created when you created the URL object.

catch (IOException e) { 
//Toast.makeText(this, "URL:No I/O Exception", Toast.LENGTH_LONG).show();

5. Add the below line in the try/catch right after you create your URL named myUrl above(in step #1). This must be in the try/catch statement.

myHttpURLConnection = (HttpURLConnection) myUrl.openConnection();

6. Compile and run!

Next Recommended Article: How to Get the Response Code Returned by a Remote HTTP server