Showing posts with label URL. Show all posts
Showing posts with label URL. Show all posts

How to Get Readable InputStream from URLConnection

public InputStream getInputStream () was added in API level 1
Returns an InputStream for reading data from the resource pointed by this URLConnection. It throws an UnknownServiceException by default. This method must be overridden by its subclasses.
Returns: the InputStream to read data from.
Throws: IOException - if no InputStream could be created.

1. Check if Response Code is OK


3. Add the below line in the try/catch created in step #1.

myInputStream = myHttpURLConnection.getInputStream();

4. Compile and run!

Resources:
http://developer.android.com/reference/java/net/URLConnection.html#getInputStream()

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 MainActivity.java file, add the below line to the imports section. 

import java.io.IOException;

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

catch (IOException e) { 
e.printStackTrace();
//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

Resources:
http://developer.android.com/reference/java/net/HttpURLConnection.html
http://stackoverflow.com/questions/16122999/java-urlconnection-when-do-i-need-to-use-the-connect-method
https://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection


How to Get a new URL Connection

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 URLConnection object named myURLConnection

3. In the MainActivity.java file, add the below line to the imports section. 

import java.io.IOException;

4. Add the below catch statement under the MalformedURLException catch you created when you created the URL object. You may choose to add Log also.

catch (IOException e) { 
e.printStackTrace();
//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.

myURLConnection = myUrl.openConnection();

6. Compile and run!

Resources:
http://developer.android.com/reference/java/net/URL.html#openConnection()

How to Declare a URLConnection object

URLConnection was added in API Level 1
A connection to a URL for reading or writing.

1. In the MainActivity.java file, add the below line to the imports section.

import java.net.URLConnection;

2. Add the below line to the onCreate method. This is declare a URLConnection named myURLConnection.

URLConnection myURLConnection;

3. Compile and run!

Resources:
http://developer.android.com/reference/java/net/URLConnection.html

How to Create a new URL object

public URL (String spec) was added in API level 1
Creates a new URL instance by parsing spec.
Throws: MalformedURLException - if spec could not be parsed as a URL.

1. Declare a URL object named myURL.

2. In the MainActivity.java file, add the below line to the imports section.

import java.net.MalformedURLException;

3. Add the below line to the onCreate method. 

try {
myUrl = new URL("http://www.androidsbs.blogspot.com");
} catch (MalformedURLException e) { 
e.printStackTrace();
//Toast.makeText(this, "URL: Malformed URL Exception", Toast.LENGTH_LONG).show();

4. Compile and run!

Next Recommended Article: How to Declare a new HttpURLConnection

How to Declare a URL object

URL was added in API Level 1
A Uniform Resource Locator(URL) that identifies the location of an Internet resource as specified by RFC 1738.

1. In the MainActivity.java file, add the below line to the imports section.

import java.net.URL;

2. In the onCreate method, add the following line. This will create a URL object named myURL.

 URL myUrl;

3. Compile and run!

Next Recommended Article: How to Create a new URL object

Resources:
http://developer.android.com/reference/java/net/URL.html