网页源码与图片查看器

由于审核机制,所以一般在子线程里获取获取资源,然后在主线程里更新UI

 

 网站源码是字符串,所以用创建一个类从输入流中获取字符串就行,图片是二进制的,就不能以获取字符串的形式,正好有BitmapFactory.decodeStream(inputStream);

可以从输入流中获取图片,返回Bitmap类型。

获取网页源码的:

 1 package cn.starry.sourcelook;
 2 
 3 import java.io.InputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.URL;
 6 
 7 import android.os.Bundle;
 8 import android.os.Handler;
 9 import android.os.Message;
10 import android.app.Activity;
11 import android.content.Context;
12 import android.text.TextUtils;
13 import android.view.View;
14 import android.view.View.OnClickListener;
15 import android.widget.Button;
16 import android.widget.EditText;
17 import android.widget.TextView;
18 import android.widget.Toast;
19 
20 public class MainActivity extends Activity implements OnClickListener {
21 
22     private Button bt_looksource;
23     private EditText et_rul;
24     private TextView tv_source;
25     private Context mContext;
26 
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31         bt_looksource = (Button) findViewById(R.id.bt_looksource);
32         et_rul = (EditText) findViewById(R.id.et_url);
33         tv_source = (TextView) findViewById(R.id.tv_source);
34         mContext = this;
35         // 二设置点击事件
36         bt_looksource.setOnClickListener(this);
37 
38     }
39 
40     // 在主线程中创建一个Handler对象
41     private Handler handler = new Handler() {
42         // 重写handleMessage方法,用来接受子线程中发来的消息
43         public void handleMessage(android.os.Message msg) {
44             String result = (String) msg.obj;
45             // 五获取服务器返回的内容,显示到textview
46             tv_source.setText(result);
47         };
48     };
49 
50     public void onClick(View v) {
51         try {
52             // 三onclik方法中获取用户输入的url地址
53             final String url_str = et_rul.getText().toString().trim();
54             if (TextUtils.isEmpty(url_str)) {
55                 Toast.makeText(mContext, "url不能为空", 0).show();
56                 return;
57             }
58 
59             new Thread(new Runnable() {
60 
61                 @Override
62                 public void run() {
63                     try {
64                         // 四请求url地址
65                         // 1.创建一个Url对象
66                         URL url = new URL(url_str);
67                         // 2.获取一个UrlConnection
68                         HttpURLConnection connection = (HttpURLConnection) url
69                                 .openConnection();
70                         // 3.为UrlConnection对象设置一些请求的参数,请求方式,连接的超时时间
71                         connection.setRequestMethod("GET");
72                         connection.setReadTimeout(10 * 1000);
73                         // 4.在获取url请求的数据前需要判断响应头
74                         int code = connection.getResponseCode();
75                         if (code == 200) {
76                             // 5.获取有效数据,并将获取的流数据解析成String
77                             InputStream inputStream = connection
78                                     .getInputStream();
79                             String result = StreamUtils
80                                     .streamToString(inputStream);
81 
82                             Message msg = new Message();
83                             msg.obj = result;
84                             handler.sendMessage(msg);
85                         }
86                     } catch (Exception e) {
87                         e.printStackTrace();
88                     }
89                 }
90             }).start();
91         } catch (Exception e) {
92             e.printStackTrace();
93         }
94     }
95 }
MainActivity.java
 1 package cn.starry.sourcelook;
 2 
 3 import java.io.ByteArrayOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 
 7 public class StreamUtils {
 8     public static String streamToString(InputStream in) {
 9         String result = "";
10         try {
11             //创建一个字节数组写入流
12             ByteArrayOutputStream out = new ByteArrayOutputStream();
13             byte[] buffer = new byte[1024];
14             int length = 0;
15             while((length = in.read(buffer)) != -1) {
16                 out.write(buffer, 0, length);
17                 out.flush();
18             }
19             result = out.toString();
20             out.close();
21         } catch (IOException e) {
22             e.printStackTrace();
23         }
24         return result;
25     }
26 }
StreamUtils.java

 

获取图片的:

 1 package cn.starry.sourcelook;
 2 
 3 import java.io.InputStream;
 4 import java.net.HttpURLConnection;
 5 import java.net.URI;
 6 import java.net.URL;
 7 
 8 import android.os.Bundle;
 9 import android.os.Handler;
10 import android.os.Message;
11 import android.app.Activity;
12 import android.content.Context;
13 import android.graphics.Bitmap;
14 import android.graphics.BitmapFactory;
15 import android.text.TextUtils;
16 import android.view.Menu;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.widget.Button;
20 import android.widget.EditText;
21 import android.widget.ImageView;
22 import android.widget.TextView;
23 import android.widget.Toast;
24 import cn.starry.piclook.R;
25 
26 public class MainActivity extends Activity implements OnClickListener {
27 
28     private Button bt_looksource;
29     private EditText et_rul;
30     private ImageView img_pic;
31     private Context mContext;
32 
33     @Override
34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         setContentView(R.layout.activity_main);
37         bt_looksource = (Button) findViewById(R.id.bt_looksource);
38         et_rul = (EditText) findViewById(R.id.et_url);
39         img_pic = (ImageView) findViewById(R.id.img_pic);
40         mContext = this;
41         // 二设置点击事件
42         bt_looksource.setOnClickListener(this);
43 
44     }
45 
46     // 在主线程中创建一个Handler对象
47     private Handler handler = new Handler() {
48         // 重写handleMessage方法,用来接受子线程中发来的消息
49         public void handleMessage(android.os.Message msg) {
50             Bitmap result = (Bitmap) msg.obj;
51             // 五获取服务器返回的内容,显示到textview
52             img_pic.setImageBitmap(result);//设置ImageView的内容
53         };
54     };
55 
56     public void onClick(View v) {
57         try {
58             // 三onclik方法中获取用户输入的url地址
59             final String url_str = et_rul.getText().toString().trim();
60             if (TextUtils.isEmpty(url_str)) {
61                 Toast.makeText(mContext, "url不能为空", 0).show();
62                 return;
63             }
64 
65             new Thread(new Runnable() {
66 
67                 @Override
68                 public void run() {
69                     try {
70                         // 四请求url地址
71                         // 1.创建一个Url对象
72                         URL url = new URL(url_str);
73                         // 2.获取一个UrlConnection
74                         HttpURLConnection connection = (HttpURLConnection) url
75                                 .openConnection();
76                         // 3.为UrlConnection对象设置一些请求的参数,请求方式,连接的超时时间
77                         connection.setRequestMethod("GET");
78                         connection.setReadTimeout(10 * 1000);
79                         // 4.在获取url请求的数据前需要判断响应头
80                         int code = connection.getResponseCode();
81                         if (code == 200) {
82                             // 5.获取有效数据,并将获取的流数据解析成String
83                             InputStream inputStream = connection
84                                     .getInputStream();
85                             Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
86                             Message msg = Message.obtain();//获取一个Message对象,内部实现是:如果之前的MEssage存在直接返回,不存在创建新的Message返回
87                             msg.obj = bitmap;
88                             handler.sendMessage(msg);
89                         }
90                     } catch (Exception e) {
91                         e.printStackTrace();
92                     }
93                 }
94             }).start();
95         } catch (Exception e) {
96             e.printStackTrace();
97         }
98     }
99 }
MainActivity.java

 

posted @ 2017-08-18 16:14  starry_sky  阅读(885)  评论(0编辑  收藏  举报