package com.wwz.htmlview;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.wwz.htmlview.util.StreamTools;
import android.app.Activity;
import android.app.ProgressDialog;
import android.drm.ProcessedData;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.text.TextUtils;
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 {
private EditText et_1;
private Button bt_1;
private TextView tv_1;
private ProgressDialog pd;
protected static final int SUCCESS = 1;
protected static final int ERROR = 2;
protected static final int ERROR1 = 3;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
pd.dismiss();
switch (msg.what) {
case SUCCESS:
tv_1.setText((String) msg.obj);
break;
case ERROR:
Toast.makeText(MainActivity.this, "获取html源码失败", 0).show();
break;
case ERROR1:
Toast.makeText(MainActivity.this, "无网络", 0).show();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_1 = (EditText) findViewById(R.id.et_1);
bt_1 = (Button) findViewById(R.id.bt_1);
tv_1 = (TextView) findViewById(R.id.tv_1);
bt_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String path = et_1.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, "输入的路径不能为空", 0).show();
return;
}
// 进度条
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("正在获取");
pd.show();
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
//此处是调用了另外一个工具类
String result = StreamTools.readStream(is);
Message message = Message.obtain();
message.what = SUCCESS;
message.obj=result;
handler.sendMessage(message);
} else {
Message message = Message.obtain();
message.what = ERROR;
handler.sendMessage(message);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Message message = Message.obtain();
message.what = ERROR1;
handler.sendMessage(message);
}
};
}.start();
}
});
}
}