从网络下载图片的代码,调试一直显示异常
代码如下,调试显示到httpcon.connect 就跳到异常了
public void download(View v)
{
String path="http://img6.cache.netease.com/cnews/2016/3/6/201603061342414ff73.png";
try
{
URL url=new URL(path);
HttpURLConnection httpcon=(HttpURLConnection) url.openConnection();
httpcon.setRequestMethod("GET");
httpcon.setReadTimeout(5000);
httpcon.setConnectTimeout(5000);
httpcon.connect();
if(httpcon.getResponseCode()==200)
{
InputStream is=httpcon.getInputStream();
Bitmap bm=BitmapFactory.decodeStream(is);
ImageView im=(ImageView) findViewById(R.id.mainImageView);
im.setImageBitmap(bm);
}
}
catch (Exception e)
{
Toast.makeText(MainActivity.this,"yh",Toast.LENGTH_LONG);
}
}
代码如下,调试显示到httpcon.connect 就跳到异常了
public void download(View v)
{
String path="http://img6.cache.netease.com/cnews/2016/3/6/201603061342414ff73.png";
try
{
URL url=new URL(path);
HttpURLConnection httpcon=(HttpURLConnection) url.openConnection();
httpcon.setRequestMethod("GET");
httpcon.setReadTimeout(5000);
httpcon.setConnectTimeout(5000);
httpcon.connect();
if(httpcon.getResponseCode()==200)
{
InputStream is=httpcon.getInputStream();
Bitmap bm=BitmapFactory.decodeStream(is);
ImageView im=(ImageView) findViewById(R.id.mainImageView);
im.setImageBitmap(bm);
}
}
catch (Exception e)
{
Toast.makeText(MainActivity.this,"yh",Toast.LENGTH_LONG);
}
}