【发布时间】:2012-03-24 23:08:51
【问题描述】:
下面粘贴的代码在以下行出现异常...
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
这是一个 IO 异常,上面写着:java.net.UnknownHostException: www.android.com
我检查了,这是一个有效的 URL。我从 HttpURLConnection 上的 android 文档中获得了原始代码,尽管我必须修复文档代码中的几个错误。
为什么我会收到此异常? 谢谢, 加里
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();
}
}
【问题讨论】:
标签: java