08 - Android访问网络图片和网页源码

1、读取网络图片和网页源码

public class AappActivity extends Activity {
	private EditText pathText;
	private ImageView imgView;

	private TextView webContent;
	private EditText webPath;

	@Override
	public void onCreate(Bundle savedInstanceState) { //主方法
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		pathText = (EditText) this.findViewById(R.id.imgPath);
		imgView = (ImageView) this.findViewById(R.id.img);

		webContent = (TextView) this.findViewById(R.id.webContent);
		webPath = (EditText) this.findViewById(R.id.webPath);
	}

	public void getImage(View v) { //获取图片
		//  解决 android.os.NetworkOnMainThreadExcep
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		String path = pathText.getText().toString();
		try {
			byte[] data = ImageService.getImage(path);
			Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
			imgView.setImageBitmap(bitmap);
		} catch (Exception e) {
			Log.i("ttt", "errr");
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "获取失败", 3);
		}
	}

	public void getWeb(View v) { //获取网页源码
	//  解决 android.os.NetworkOnMainThreadExcep
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		String path = webPath.getText().toString();
		String html;
		try {
			html = PageServict.getHtml(path);
			webContent.setText(html);
		} catch (Exception e) {
			webContent.setText("异常信息");
			e.printStackTrace();
		}

	}}

public class ImageService {
	public static byte[] getImage(String path) throws Exception {
		URL url = new URL(path);
		// 基于HTTp协议
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == 200) {
			InputStream is = conn.getInputStream();
			return StreamTool.read(is);
		}
		return null;
	}
}


public class PageServict {

	public static String getHtml(String path) throws Exception {
		URL url = new URL(path);
		// 基于HTTp协议
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		if (conn.getResponseCode() == 200) {
			InputStream is = conn.getInputStream();
			//XmlPullParser 可以解析imputstream 得到对象用listview显示
			byte[] html = StreamTool.read(is);
			return  new String(html,"UTF-8");
		}
		return null;
	}

}

public class StreamTool {
	//读取流中数据
	public static byte[] read(InputStream is) throws Exception {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = is.read(buffer)) != -1) {
			bos.write(buffer, 0, len);
		}
		is.close();
		return bos.toByteArray();

	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="图片路径" /> 
    <EditText 
        android:text="http://www.baidu.com/img/baidu_sylogo1.gif"
        android:id="@+id/imgPath" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="getImage"
        android:text="获取图片" />
	<ImageView 
	    android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/img"/>
	<EditText 
        android:text="http://www.baidu.com"
        android:id="@+id/webPath" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
	<Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:onClick="getWeb"
        android:text="获取网页源码" />
	 <ScrollView 
	     android:layout_height="wrap_content"
        android:layout_width="wrap_content">
	    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="网页源码"
        android:id="@+id/webContent" />
	   </ScrollView>  
</LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值