package com.example.pulls; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.example.pulls.uri.Cantant; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button bt_gethtml, bt_pullImage; public static final String pathHtml = Cantant.ipConfig + "/login.html"; public static final String pathImg = Cantant.ipConfig + "/huang.jpg"; TextView textView; ImageView Iv_imag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_gethtml = (Button) findViewById(R.id.bt_gethtml); textView = (TextView) findViewById(R.id.textView); bt_pullImage = (Button) findViewById(R.id.bt_pullImage); Iv_imag = (ImageView) findViewById(R.id.img); bt_gethtml.setOnClickListener(this); bt_pullImage.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_gethtml: getHtmLCode(); break; case R.id.bt_pullImage: getImage(); break; } } Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { //主线程 switch (msg.what) { case 1: String html = (String) msg.obj; textView.setText(html); break; case 2: Toast.makeText(MainActivity.this, "没有数据", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show(); break; case 4: Bitmap bitmap = (Bitmap) msg.obj; Iv_imag.setImageBitmap(bitmap); break; } } }; //得到图片 public void getImage() { new Thread() { @Override public void run() { try { Bitmap imag = getImage(pathImg); if (imag != null) { Message msg = new Message(); msg.obj = imag; msg.what = 4; mHandler.sendMessage(msg); } else { Message msg = new Message(); msg.what = 2; mHandler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); Message msg = new Message(); msg.what = 3; mHandler.sendMessage(msg); } } }.start(); } //解析图片 public Bitmap getImage(String path) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(3000); conn.setRequestMethod("GET"); //得到请求码 int code = conn.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); return BitmapFactory.decodeStream(inputStream); } return null; } //得到网页源码 public void getHtmLCode() { //联网必须开展子线程 new Thread() { @Override public void run() { try { String html = getHtml(pathHtml); if (html != null) { //更新ui必须在主线程中 //用Handler处理:能够在主线程和子线程中通信 //html封装成消息Message Message msg = new Message(); msg.obj = html; //数据 msg.what = 1;//标识 mHandler.sendMessage(msg); } else { Message msg = new Message(); msg.what = 2; mHandler.sendMessage(msg); } } catch (Exception e) { Message msg = new Message(); msg.what = 3; mHandler.sendMessage(msg); e.printStackTrace(); } } }.start(); } //得到数据流 public String getHtml(String path) throws Exception { //封装path URL url = new URL(path); //打开连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置超时 conn.setConnectTimeout(5000); //设置请求方式 conn.setRequestMethod("GET"); //得到响应码 int code = conn.getResponseCode(); //判断 if (code == 200) { //获得数据流 InputStream is = conn.getInputStream(); String s = null; try { s = parseHtml(is); } catch (Exception e) { e.printStackTrace(); } return s; } return null; } //解析数据流 private String parseHtml(InputStream is) throws Exception { //构建一个输出流对象 ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } String s = baos.toString(); baos.close(); return s; } }
布局xml
<?xml version="1.0" encoding="utf-8"?> <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="wrap_content" android:orientation="vertical"> <Button android:id="@+id/bt_gethtml" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获得html源码" /> <Button android:id="@+id/bt_pullImage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获得图片" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>