package com.ljq.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.Test;
public classInternetTest {
@SuppressWarnings("static-access")
@Test
public voidgetImg() throwsException {
String urlPath = "http://i0.itc.cn/20101116/62d_67fd2b2a_207d_4de2_a4f9_fb93cc8da492_0.jpg";
URL url = newURL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(6*1000);// 注意要设置超时,设置时间不要超过10秒,避免被android系统回收
if (conn.getResponseCode()!= 200)throw new RuntimeException("请求url失败");
InputStream inSream = conn.getInputStream();
//把图片保存到项目的根目录
new InternetTest().readAsFile(inSream,new File("sohu.jpg"));
}
public staticvoid readAsFile(InputStream inSream, File file)throws Exception{
FileOutputStream outStream = new FileOutputStream(file);
byte[] buffer= new byte[1024];
int len= -1;
while( (len= inSream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inSream.close();
}
}
1、由于访问网络,所以要在AndroidManifest.xml中加入访问因特网服务的权限
<uses-permission android:name="android.permission.INTERNET" />
如果不加入的话,就会出现permission denied的异常
2、开始编写代码,代码如下
package org.loulijun.HttpGet;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HttpGet extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
String myString=null;
try
{
//定义获取文件内容的URL
URL myURL=new URL("http://baike.baidu.com/view/46765.htm");
//打开URL链接
URLConnection ucon=myURL.openConnection();
//使用InputStream,从URLConnection读取数据
InputStream is=ucon.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
//用ByteArrayBuffer缓存
ByteArrayBuffer baf=new ByteArrayBuffer(50);
int current=0;
while((current=bis.read())!=-1)
{
baf.append((byte)current);
}
//将缓存的内容转化为String,用UTF-8编码
myString=EncodingUtils.getString(baf.toByteArray(), "UTF-8");
}catch(Exception e)
{
myString=e.getMessage();
}
//设置屏幕显示
tv.setText(myString);
this.setContentView(tv);
}
}
3、首先,我们需要URL类,表示我们要获取内容的网址
URL myURL=new URL(HTTP://www.baidu.com/hello.txt);
4、同时,用类URLConnection表示一个打开的网络连接
URLConnection ucon=myURL.openConnection();
5、对于从网络上读到的数据,用字节流的形式表示
InputStream is=ucon.getInputStream();
为了避免频繁读取字节流,提高读取效率,用BufferedInputStream缓存读到的字节流
InputStream is=ucon.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
6、准备好BufferdInputStream后,我们就可以用read方法读入网络数据
ByteArrayBuffer baf=new ByteArrayBuffer(50);
int current=0;
while((current=bis.read())!=-1)
{
baf.append((byte)current);
}
7、由于读到的数据只是字节流,无法直接显示到屏幕上,所以我们得在显示之前将字节流转换为可读取的字符串
myString=EncodingUtils.getString(baf.toByteArray(),"UTF-8");
如果读取的是.txt等文件是UTF-8格式的,就需要对数据进行专门的转换