The code pasted below gets an exception on the line that says…
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
It’s an IO exception that says: java.net.UnknownHostException: http://www.android.com
I checked and that is a valid URL. I got the original code from the android docs on HttpURLConnection although I had to fix several bugs in the docs code.
Why am I getting this exception?
thanks,
Gary
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpURLConnection urlConnection = null;
try
{
URL url = new URL("http://www.android.com/");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
urlConnection.disconnect();
}
}
Does your app have the Internet access ?try this in your AndroidManaifest.xml file