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

J2SE实现网络图片的获取

 public static void main(String[] args) throws Exception {
       String path = "http://res.img.ifeng.com/2011/1219/xes_bb10a1495fa8e7f76415023d177585d0.jpg";
      
       URL url = new URL(path);
      
       HttpURLConnection conn = (HttpURLConnection) url.openConnection();
       conn.setRequestMethod("GET");
       conn.setConnectTimeout(5*1000);
       InputStream inStream = conn.getInputStream();
      
       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
       byte[] buffer = new byte[1024];
      
       int len=0;
       while((len=inStream.read(buffer))!=-1){
           outStream.write(buffer,0,len);
       }
      
       byte[] data = outStream.toByteArray();
      
       File f = new File("pic.jpg");
       FileOutputStream fos = new FileOutputStream(f);
       fos.write(data);
       fos.close();
    }


Androd中获取网络图片

资源

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


布局

<Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_text"
        android:id="@+id/showBtn"
        />
   
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        />  


添加ImageService类

package cn.class3g.service;
…
public class ImageService {
    public static byte[] getImage(String path) throws Exception{
       URL url = new URL(path);
       //get //post
       HttpURLConnection conn = (HttpURLConnection)url.openConnection();
       conn.setRequestMethod("GET");
       conn.setConnectTimeout(5*1000);
       InputStream inStream = conn.getInputStream();
      
       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
       byte[] buffer = new byte[1024];
       int len = 0;
       while( (len = inStream.read(buffer)) !=-1 ){
           outStream.write(buffer, 0, len);
       }
       byte[] data = outStream.toByteArray();//图片的二进制数据
       outStream.close();
       inStream.close();
       return data;
    }
}


Activity

public void onClick(View v) {
       String path = "http://res.img.ifeng.com/2011/1221/xes_5a3a1d15ffb4b856a02237cdd79d4e41.jpg";
 
       try {
           byte[] data = ImageService.getImage(path);
 
           Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
           imgView.setImageBitmap(bitmap);
          
       } catch (Exception e) {
//         e.printStackTrace();
           Log.e("TAG", e.toString());
           Toast.makeText(this, R.string.error, 1).show();
       }
    }


Androd中获取网页代码

布局

 <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_text"
        android:id="@+id/showBtn"
        />
   
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        />  


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

HtmlService

public class HtmlService {
    /**
     * 获取给定路径的html代码
     * @param path 网页路径
     * @return
     * @throws Exception
     */
    public static String getHtml(String path) throws Exception{
       URL url = new URL(path);
       //get //post
       HttpURLConnection conn = (HttpURLConnection)url.openConnection();
       conn.setRequestMethod("GET");
       conn.setConnectTimeout(5*1000);
       InputStream inStream = conn.getInputStream();
      
       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
       byte[] buffer = new byte[1024];
       int len = 0;
       while( (len = inStream.read(buffer)) !=-1 ){
           outStream.write(buffer, 0, len);
       }
       byte[] data = outStream.toByteArray();//网页的二进制数据
       outStream.close();
       inStream.close();
       return new String(data, "gb2312");
    }
}


ShowHtmlActivity

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

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值