Android应用开发之获取网络数据


J2SE实现网络图片的获取

  1. public static void main(String[] args) throws Exception {  
  2.       String path = "http://img.bkjia.com/img/Android.png";  
  3.        
  4.       URL url = new URL(path);  
  5.        
  6.       HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  7.       conn.setRequestMethod("GET");  
  8.       conn.setConnectTimeout(5*1000);  
  9.       InputStream inStream = conn.getInputStream();  
  10.        
  11.       ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  12.       byte[] buffer = new byte[1024];  
  13.        
  14.       int len=0;  
  15.       while((len=inStream.read(buffer))!=-1){  
  16.           outStream.write(buffer,0,len);  
  17.       }  
  18.        
  19.       byte[] data = outStream.toByteArray();  
  20.        
  21.       File f = new File("pic.jpg");  
  22.       FileOutputStream fos = new FileOutputStream(f);  
  23.       fos.write(data);  
  24.       fos.close();  
  25.    }  

Androd中获取网络图片

资源

  1. <string name="btn_text">显示网络图片</string>  
  2.    <string name="error">下载图片失败!!</string>  

布局

  1. <Button  
  2.         android:layout_width="fill_parent"  
  3.         android:layout_height="wrap_content"  
  4.         android:text="@string/btn_text"  
  5.         android:id="@+id/showBtn"  
  6.         />  
  7.      
  8.     <ImageView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/imageView"  
  12.         />    

添加ImageService类

  1. package cn.class3g.service;  
  2. …  
  3. public class ImageService {  
  4.     public static byte[] getImage(String path) throws Exception{  
  5.        URL url = new URL(path);  
  6.        //get //post  
  7.        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  8.        conn.setRequestMethod("GET");  
  9.        conn.setConnectTimeout(5*1000);  
  10.        InputStream inStream = conn.getInputStream();  
  11.         
  12.        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  13.        byte[] buffer = new byte[1024];  
  14.        int len = 0;  
  15.        while( (len = inStream.read(buffer)) !=-1 ){  
  16.            outStream.write(buffer, 0, len);  
  17.        }  
  18.        byte[] data = outStream.toByteArray();//图片的二进制数据  
  19.        outStream.close();  
  20.        inStream.close();  
  21.        return data;  
  22.     }  
  23. }  

Activity

  1. public void onClick(View v) {  
  2.        String path = "http://img.bkjia.com/img/Android.png";  
  3.    
  4.        try {  
  5.            byte[] data = ImageService.getImage(path);  
  6.    
  7.            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  
  8.            imgView.setImageBitmap(bitmap);  
  9.             
  10.        } catch (Exception e) {  
  11. //         e.printStackTrace();  
  12.            Log.e("TAG", e.toString());  
  13.            Toast.makeText(this, R.string.error, 1).show();  
  14.        }  
  15.     }  

Androd中获取网页代码

布局

  1. <Button  
  2.        android:layout_width="fill_parent"  
  3.        android:layout_height="wrap_content"  
  4.        android:text="@string/btn_text"  
  5.        android:id="@+id/showBtn"  
  6.        />  
  7.     
  8.    <ImageView  
  9.        android:layout_width="wrap_content"  
  10.        android:layout_height="wrap_content"  
  11.        android:id="@+id/imageView"  
  12.        />    

代码:在前面的ImageService.getImage()方法基础上修改即可

HtmlService

  1. public class HtmlService {  
  2.     /**  
  3.      * 获取给定路径的html代码  
  4.      * @param path 网页路径  
  5.      * @return  
  6.      * @throws Exception  
  7.      */  
  8.     public static String getHtml(String path) throws Exception{  
  9.        URL url = new URL(path);  
  10.        //get //post  
  11.        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  12.        conn.setRequestMethod("GET");  
  13.        conn.setConnectTimeout(5*1000);  
  14.        InputStream inStream = conn.getInputStream();  
  15.         
  16.        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  17.        byte[] buffer = new byte[1024];  
  18.        int len = 0;  
  19.        while( (len = inStream.read(buffer)) !=-1 ){  
  20.            outStream.write(buffer, 0, len);  
  21.        }  
  22.        byte[] data = outStream.toByteArray();//网页的二进制数据  
  23.        outStream.close();  
  24.        inStream.close();  
  25.        return new String(data, "gb2312");  
  26.     }  
  27. }  

ShowHtmlActivity

  1. public void onClick(View v) {  
  2.     String path = pathText.getText().toString();  
  3.     try {  
  4.        String htmlcode = HtmlService.getHtml(path);  
  5.        resultView.setText(htmlcode);  
  6.     } catch (Exception e) {  
  7.        Log.e(TAG, e.toString());  
  8.        Toast.makeText(ShowHtmlActivity.this, R.string.error, 1).show();  
  9.     }  
  10. }  

相关内容