StreamTool.java:

package util;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTool {/** * 把一个inputstream里面的内容转化成一个byte[] */public static byte[] getBytes(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();bos.flush();byte[] result = bos.toByteArray();System.out.println(new String(result));return result;}}
NetUtil.java:

package service;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import util.StreamTool;import android.widget.Toast;public class NetUtil {public static String getHtml(String address) throws Exception {URL url = new URL(address);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setReadTimeout(5000);conn.setRequestMethod("GET");int code = conn.getResponseCode();if (code == 200) {InputStream is = conn.getInputStream();byte[] result = StreamTool.getBytes(is);return new String(result);} else {throw new IllegalStateException("访问失败");}}}
MainActivity.java:

package com.example.httpviewer;import service.NetUtil;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private EditText mEtAddress;private Button mBtView;private TextView mTvView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mBtView = (Button) this.findViewById(R.id.chakan);mEtAddress = (EditText) this.findViewById(R.id.editText1);mTvView = (TextView) this.findViewById(R.id.TextView1);mBtView.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.chakan:// 按钮对应的点击事件String address = mEtAddress.getText().toString().trim();if ("".equals(address)) {Toast.makeText(this, "地址不能为空", Toast.LENGTH_SHORT).show();return;}try {String html = NetUtil.getHtml(address);mTvView.setText(html);} catch (Exception e) {e.printStackTrace();Toast.makeText(this, "获取数据失败", 0).show();}break;}}}
main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/input_address" />    <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:lines="2"        android:text="http://www.baidu.com" />    <Button        android:id="@+id/chakan"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/go" />    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent" >        <TextView            android:id="@+id/TextView1"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:scaleType="center" />    </ScrollView></LinearLayout>

更多相关文章

  1. android:自定义长按/长点击事件
  2. Android在ListView中获得对应行的button点击事件
  3. 多个按钮左右相互挤压效果
  4. android ListView的常用事件
  5. 事件总线EventBus Android开源库的使用
  6. android listview的item里面的imageview的点击事件
  7. Notification 事件 使用
  8. Android Material Design按钮样式设计

随机推荐

  1. android实现xml数据的解析
  2. Android CalendarView 使用
  3. Android DOM解析XML
  4. Android自定义弹窗进度条
  5. ch029 Android service aidl
  6. Accessing internal data on Android dev
  7. Android(安卓)APK反编译
  8. 【工具类】如何通过代码安装一个apk文件
  9. Android 解析Html
  10. Android Bluetooth UUID