在AndroidManifest.xml里面先添加访问网络的权限:

<uses-permission android:name="android.permission.INTERNET"/>

效果图如下

下面是主要代码:

 package com.hb.neting;

 import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast; public class MainActivity extends Activity {
private ImageView iv_show;
private EditText et_input;
private String path;
private int code;
private HttpURLConnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_show=(ImageView) findViewById(R.id.iv_show);
et_input=(EditText) findViewById(R.id.et_inpput);
}
@SuppressLint("ShowToast") public void chakan(View view){
path = et_input.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, "不能输入空的", 0).show();
return;
}
new Thread(){
public void run() {
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
code = conn.getResponseCode();
if(code==200){
InputStream in = conn.getInputStream();
//解析图片
final Bitmap stream = BitmapFactory.decodeStream(in);
runOnUiThread(new Runnable() {
public void run() {
//更新UI
iv_show.setImageBitmap(stream);
}
});
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}

这是xml的布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/et_inpput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入获取图片的地址:" />
<Button
android:id="@+id/bt_read"
android:onClick="chakan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看"
/>
<ImageView
android:id="@+id/iv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

源码:http://pan.baidu.com/s/1bUgMgY

接着看一下网页源码查看器的小案例:

  既然都涉及到网络的添加一个如上的网络权限是必不可少的了,具体操做如上所示,先看效果图:

主要代码:

 package com.hb.network;

 import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import com.hb.utils.ReadStreamUtils; public class MainActivity extends Activity {
protected static final int SUCESS = 0;
protected static final int EORR = 1;
private TextView tv_show;
private EditText et_input;
private URL url;
private String path;
@SuppressLint("HandlerLeak")
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SUCESS:
String content=(String) msg.obj;
tv_show.setText(content);
break; case EORR:
Toast.makeText(MainActivity.this,"查看源码失败" , 0).show();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_show=(TextView) findViewById(R.id.tv_show);
et_input=(EditText) findViewById(R.id.et_input); }
public void onclick(View view){
path = et_input.getText().toString().trim();
if(TextUtils.isEmpty(path)){
return;
}new Thread(){
public void run() {
try {
url = new URL(path);
//判断从EditText获取的数据否为空
if(TextUtils.isEmpty(path)){
return;
}
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
InputStream is= conn.getInputStream();
String content = ReadStreamUtils.Read(is);
Message msg = new Message();
msg.what=SUCESS;
msg.obj=content;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what=EORR;
handler.sendMessage(msg);
}
};
}.start(); }
}
 package com.hb.utils;

 import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; public class ReadStreamUtils {
/**
* 读取流的输入
* @param is
* @return
* @throws IOException
*/
public static String Read(InputStream is) throws IOException{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte [] buffer=new byte[1024];
while((len=is.read(buffer))!=-1){
bos.write(buffer,0,len);
}
is.close();
bos.close();
String temp = bos.toString();
if(temp.contains("charset=utf-8")){
return bos.toString("utf-8");
}else if(temp.contains("charset=iso-8859-1")){
return bos.toString("iso-8859-1");
}
return null; } }

及xml布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" > <EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要查看源码的网址:" /> <Button
android:onClick="onclick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看"
android:textSize="25sp" /> <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView> </LinearLayout>

  源码:http://pan.baidu.com/s/1bUgMgY

     http://pan.baidu.com/s/1i46RQqL 

Android 网络图片查看器与网页源码查看器的更多相关文章

  1. 黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器

    1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来 后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序 ...

  2. Android(java)学习笔记203:网页源码查看器(Handler消息机制)

    1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...

  3. Android(java)学习笔记146:网页源码查看器(Handler消息机制)

    1.项目框架图: 2.首先是布局文件activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com ...

  4. android 网络_网络源码查看器

    xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  5. Android: 在WebView中获取网页源码

    1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 final c ...

  6. 编译Android 4.4.4 r1的源码刷Nexus 5手机详细教程

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/54562606 网上关于编译Android源码的教程已经很多了,但是讲怎么编译And ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  8. 如何查看google chrome 插件源码

    常用浏览器google chrome 有很多优秀的插件,寂寞的时候想看看人家是怎么实现的,说是快那就动手吧 插件代码位置 本人mac笔记本,chrome 插件位置如下 $ cd  /Users/vin ...

  9. Python爬虫学习之获取网页源码

    偶然的机会,在知乎上看到一个有关爬虫的话题<利用爬虫技术能做到哪些很酷很有趣很有用的事情?>,因为强烈的好奇心和觉得会写爬虫是一件高大上的事情,所以就对爬虫产生了兴趣. 关于网络爬虫的定义 ...

随机推荐

  1. 使用Tornado实现http代理

    0x00 http代理 http代理的用处非常多,市面上也有公开的代理,可是有时候为了工作须要,比方分析应用层流量.做数据訪问控制.甚至做监控等等.Tornado提供了一些非常方便的环境和API,我们 ...

  2. [TypeScript] Overload a Function with TypeScript’s Overload Signatures

    Some functions may have different return types depending on the types of the arguments with which th ...

  3. ajax请求锁屏功能

    我们有时候在进行ajax请求的时候希望页面不允许点击,等请求结束之后才可以进行点击,那么可以写: $(".cloudos-container").ajaxStart($.block ...

  4. Java 编码 UTF-8

    近期在处理文件时发现了相同类型的文件使用的编码可能是不同的.所以想将文件的格式统一一下(由于UTF-8的通用性,决定往UTF-8统一),遇见的第一个问题是:怎样查看现有文件的编码方式. 文件编码问题集 ...

  5. 跟踪oracle中sql语句运行过程及相关知识拓展

    select * from v$sqlarea; select * from v$sqlarea where first_load_time>'2010-11-27/09:30:00'; 这种方 ...

  6. 20170623_oracle_优化与体系结构

    一般优化技巧 建议不用"*"代替所有列名 删除所有数据用TRUNCATE代替DELETE 用NOT EXISTS 代替NOT IN 用EXISTS代替IN 用EXISTS代替DIS ...

  7. ios32---线程的状态

    // // ViewController.m // 04-了解-线程的状态 // // 创建线程是处于新建状态,start是就绪状态,会放入到可调度线程池里面(cpu看线程是否可以调用,是看线程是否在 ...

  8. Keil和IAR——使用笔记

    1. 几个宏的定义 Keil和IAR均用到以下三种宏定义,分别是:USE_STDPERIPH_DRIVER——表示使用ST提供的标准外设固件库:STM32F40_41xxx——作为芯片的判断:VECT ...

  9. ARM+linux系统移植3G拨号上网收发短信(三)【转】

    本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17099755 一.用text查看模式下面的“发”是指我敲的命令,“收”是指回车后显 ...

  10. ModuleNotFoundError: No module named 'PIL'

    错误:ModuleNotFoundError: No module named 'PIL' 解决办法: pip install Pillow