android URL

1、先在AndroidManifest.xml中注册加入访问因特网服务的权限: 
<uses-permission android:name="android.permission.INTERNET" /> 
(若不加入,则会出现permission denied异常) 

2、代码如下: 
Java代码   收藏代码
  1. package vip.test.HttpGet;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.InputStream;  
  4. import java.net.URL;  
  5. import java.net.URLConnection;  
  6. import org.apache.http.util.ByteArrayBuffer;  
  7. import org.apache.http.util.EncodingUtils;  
  8. import vip.test.HttpGet.R;  
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.widget.TextView;  
  12. public class HttpGet extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         TextView tv = new TextView(this);  
  19.         String myString = null;  
  20.         try {  
  21.             // 定义获取文件内容的URL  
  22.             URL myURL = new URL(  
  23.                     "HTTP://www.baidu.com/hello.txt&quot");  
  24.             // 打开URL链接  
  25.             URLConnection ucon = myURL.openConnection();  
  26.             // 使用InputStream,从URLConnection读取数据  
  27.             InputStream is = ucon.getInputStream();  
  28.             BufferedInputStream bis = new BufferedInputStream(is);  
  29.             // 用ByteArrayBuffer缓存  
  30.             ByteArrayBuffer baf = new ByteArrayBuffer(50);  
  31.             int current = 0;  
  32.             while ((current = bis.read()) != -1) {  
  33.                 baf.append((byte) current);  
  34.             }  
  35.             // 将缓存的内容转化为String,用UTF-8编码  
  36.             myString = EncodingUtils.getString(baf.toByteArray(), "UTF-8");  
  37.         } catch (Exception e) {  
  38.             myString = e.getMessage();  
  39.         }  
  40.         // 设置屏幕显示  
  41.         tv.setText(myString);  
  42.         this.setContentView(tv);  
  43.     }  
  44. }  


3、代码解释: 

1)实例URL类:myURL,表示要获取内容的网址: 
URL myURL=new URL(HTTP://www.baidu.com/hello.txt); 

2)实例URLConnection类,表示一个打开的网络连接ucon: 
URLConnection ucon=myURL.openConnection(); 

3)用字节流的形式表示从网络上读到的数据: 
InputStream is=ucon.getInputStream(); 
   为避免频繁读取字节流,提高读取效率,用BufferedInputStream缓存读到的字节流 
InputStream is=ucon.getInputStream(); 
BufferedInputStream bis=new BufferedInputStream(is); 

4)用read方法读入网络数据: 
ByteArrayBuffer baf=new ByteArrayBuffer(50); 
int current=0; 
while((current=bis.read())!=-1) 

baf.append((byte)current); 


5)由于读到的数据只是字节流,无法直接显示到屏幕上,所以得在显示之前将字节流转换为可读取的字符串: 
myString=EncodingUtils.getString(baf.toByteArray(),&quot;UTF-8&quot;); 

(如果读取的是.txt等文件是UTF-8格式的,就需要对数据进行专门的转换) 


地址来自http://vinny-w.iteye.com/blog/1327743

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值