public class StreamTool {
/**
* 读取流中的数据
* @param inputStream
* @return
* @throws Exception
*/
public static byte[] read(InputStream inputStream)throws Exception{
ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
arrayOutputStream.write(buffer, 0, len);
}
arrayOutputStream.close();
return arrayOutputStream.toByteArray();
}
}
public class PageService {
/**
* 获取网页HTML源代码
* @param path 网页路径
* @return
*/
public static String getHtml(String path)throws Exception{
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inputStream=conn.getInputStream();
byte[] data=StreamTool.read(inputStream);
String html=new String(data,"UTF-8");
return html;
}
return null;
}
}
public class MainActivity extends Activity {
private Button btnView;
private EditText pagepathEt;
private TextView codeView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnView=(Button)findViewById(R.id.btnView);
pagepathEt=(EditText)findViewById(R.id.pagepath);
codeView=(TextView)findViewById(R.id.codeView);
btnView.setOnClickListener(new ButtonOnClickListener());
}
private final class ButtonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String path=pagepathEt.getText().toString();
try {
String html= PageService.getHtml(path);
codeView.setText(html);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pagepath"
/>
<EditText
android:text="http://www.tuge.com"
android:layout_width="match_parent"
android:id="@+id/pagepath"
android:layout_height="wrap_content"/>
<Button
android:text="@string/btnView"
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/codeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
/**
* 读取流中的数据
* @param inputStream
* @return
* @throws Exception
*/
public static byte[] read(InputStream inputStream)throws Exception{
ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
arrayOutputStream.write(buffer, 0, len);
}
arrayOutputStream.close();
return arrayOutputStream.toByteArray();
}
}
public class PageService {
/**
* 获取网页HTML源代码
* @param path 网页路径
* @return
*/
public static String getHtml(String path)throws Exception{
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inputStream=conn.getInputStream();
byte[] data=StreamTool.read(inputStream);
String html=new String(data,"UTF-8");
return html;
}
return null;
}
}
public class MainActivity extends Activity {
private Button btnView;
private EditText pagepathEt;
private TextView codeView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnView=(Button)findViewById(R.id.btnView);
pagepathEt=(EditText)findViewById(R.id.pagepath);
codeView=(TextView)findViewById(R.id.codeView);
btnView.setOnClickListener(new ButtonOnClickListener());
}
private final class ButtonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String path=pagepathEt.getText().toString();
try {
String html= PageService.getHtml(path);
codeView.setText(html);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pagepath"
/>
<EditText
android:text="http://www.tuge.com"
android:layout_width="match_parent"
android:id="@+id/pagepath"
android:layout_height="wrap_content"/>
<Button
android:text="@string/btnView"
android:id="@+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/codeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>