加入收藏 | 设为首页 | 会员中心 | 我要投稿 三明站长网 (https://www.0598zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Android开发教程:借助http协议获取网络图片

发布时间:2021-11-24 13:55:58 所属栏目:教程 来源:互联网
导读:http用于传输WWW方式的数据。http协议采用了请求响应的模型。在Android中提供了HttpURLConnection和HttpClient接口开发HTTP程序。下面分别使用这两种方式获取网络图片。 1.HttpURLConnection 代码如下: [html] public class HttpURLConnectionActivity exte

http用于传输WWW方式的数据。http协议采用了请求响应的模型。在Android中提供了HttpURLConnection和HttpClient接口开发HTTP程序。下面分别使用这两种方式获取网络图片。
1.HttpURLConnection
 
代码如下:   
 
[html]
public class HttpURLConnectionActivity extends Activity {  
  
    private ImageView imageView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.simple1);  
          
        imageView=(ImageView) this.findViewById(R.id.imageView1);  
        //传入网络图片地址  
        try {  
            URL url = new URL("http://news.xinhuanet.com/photo/2012-02/09/122675973_51n.jpg");  
            HttpURLConnection conn= (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setConnectTimeout(5*1000);  
            conn.connect();  
            InputStream in=conn.getInputStream();  
            ByteArrayOutputStream bos=new ByteArrayOutputStream();  
            byte[] buffer=new byte[1024];  
            int len = 0;  
            while((len=in.read(buffer))!=-1){  
                bos.write(buffer,0,len);  
            }  
            byte[] dataImage=bos.toByteArray();  
            bos.close();  
            in.close();  
            Bitmap bitmap=BitmapFactory.decodeByteArray(dataImage, 0, dataImage.length);  
            //Drawable drawable=BitmapDrawable.  
            imageView.setImageBitmap(bitmap);  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            Toast.makeText(getApplicationContext(), "图片加载失败", 1).show();  
        }  
          
    }  
}  
最后不要忘记在manifest.xml加入网络访问权限:
[html]
<uses-permission android:name="android.permission.INTERNET" />  
         由于访问网络图片是比较耗时的操作,所以在正式项目中使用异步加载图片,效果会更好。
         运行效果:
 
        
 
         2.HttpClient
 
          下面使用HttpClient获取网页内容:
 
        
 
[html]
public class HttpClientActivity extends Activity {  
  
    private ImageView imageview;  
    private TextView text;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.simple2);  
          
        imageview=(ImageView) this.findViewById(R.id.imageView2);  
        text=(TextView) this.findViewById(R.id.textView2);  
        HttpGet httpGet=new HttpGet("http://cloud.csdn.net/a/20120209/311628.html");  
        HttpClient httpClient=new DefaultHttpClient();  
        try {  
            //得到HttpResponse对象  
            HttpResponse httpResponse=httpClient.execute(httpGet);  
            //HttpResponse的返回结果是不是成功  
            if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){  
                //得到返回数据的字符串  
                String dataImageStr=EntityUtils.toString(httpResponse.getEntity());  
                text.setText(dataImageStr);  
            }  
        } catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
}  

(编辑:三明站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!